Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Improve template android #25

Merged
merged 22 commits into from
Sep 12, 2017
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions bin/gyro
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

abort 'error: gyro requires Ruby 2 or higher.' if RUBY_VERSION < '2.0.0'

$LOAD_PATH.unshift File.expand_path('../lib', File.dirname(__FILE__)) if $PROGRAM_NAME == __FILE__
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is no longer be useful. If you want to test gyro locally, please use the following lines :

gem build gyro.gemspec
gem install gyro-1.0.0.gem

Copy link
Contributor

@StevenWatremez StevenWatremez Sep 12, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or you can use an other command when you develop gyro. Go to gyro repo and launch :

ruby -Ilib bin/gyro -m <model> -t android -o <output>


require 'gyro'
require 'optparse'
require 'pathname'
Expand Down
32 changes: 16 additions & 16 deletions lib/templates/android/entity.liquid
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
{%- capture imports -%}
{%- if params.package.size > 0 -%}
package {{ params.package }};
{% endif -%}
{%- endif %}

/* DO NOT EDIT | Generated by gyro */
{{ empty_line }}
{%- if entity.has_json_key_path == true %}
import com.google.gson.annotations.SerializedName;
{%- endif -%}
{% comment %} *** Empty line *** {% endcomment %}
{%- endif %}
{%- if entity.has_date_attribute == true %}
import java.util.Date;
{%- endif -%}
{%- endif %}
{%- if entity.has_list_relationship == true %}
import java.util.List;
{%- endif -%}
{%- endif %}
{%- if entity.has_date_attribute == true or entity.has_list_relationship == true %}
{{ empty_line }}
{%- endif %}
{%- if entity.has_list_attributes == true %}
import io.realm.RealmList;
{%- endif %}
import io.realm.RealmObject;
{%- if entity.has_ignored == true or entity.has_enum_attributes == true %}
{%- if entity.has_ignored == true %}
import io.realm.annotations.Ignore;
{%- endif -%}
{%- endif %}
{%- if entity.has_indexed_attributes == true %}
import io.realm.annotations.Index;
{%- endif -%}
{%- endif %}
{%- if entity.has_primary_key == true %}
import io.realm.annotations.PrimaryKey;
{%- endif %}
{%- endcapture -%}

{{ imports }}

/* DO NOT EDIT | Generated by gyro */

{% if entity.comment.size > 0 -%}
/**
* {{ entity.comment }}
*/
{%- endif %}
{% endif %}
{%- assign primary_key = entity.identity_attribute -%}
public class {{ entity.name }} extends RealmObject {

{%- include 'inc/attributes_enum' %}
{%- include 'inc/relationships_enum' %}
{%- include 'inc/attributes_properties' %}
{% include 'inc/attributes_properties' %}
{%- include 'inc/relationships_properties' %}
{%- include 'inc/attributes_getter_setter' %}
{%- include 'inc/relationships_getter_setter' %}
{%- include 'inc/enum_getter_setter' %}

}
6 changes: 3 additions & 3 deletions lib/templates/android/enum.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{%- if params.package.size > 0 -%}
package {{ params.package }};
{%- endif %}

/* DO NOT EDIT | Generated by gyro */

public enum {{ attribute.enum_type }} {
Expand All @@ -22,7 +23,7 @@ public enum {{ attribute.enum_type }} {
{{ attribute.enum_type }}(final String jsonValue) {
this.jsonValue = jsonValue;
}
{% if params.support_annotations.size > 0 -%}
{% if params.support_annotations.size > 0 %}
@android.support.annotation.Nullable
{%- endif %}
public static {{ attribute.enum_type }} get(final String jsonValue) {
Expand All @@ -33,12 +34,11 @@ public enum {{ attribute.enum_type }} {
}
return null;
}
{% if params.support_annotations.size > 0 -%}
{% if params.support_annotations.size > 0 %}
@android.support.annotation.NonNull
{%- endif %}
public String getJsonValue() {
return jsonValue;
}

}
{%- endif %}
15 changes: 8 additions & 7 deletions lib/templates/android/inc/_attributes_enum.liquid
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
{%- if entity.attributes.size > 0 %}

public static class Attributes {
private Attributes() {
// Hide constructor
}
{% for attribute in entity.attributes -%}
public static final class Attributes {
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_ignored == false or attribute.read_only == false -%}
{%- if attribute.comment.size > 0 -%}
/** {{ attribute.comment }} */
{%- if attribute.comment.size > 0 %}
// {{ attribute.comment }}
{%- endif %}
public static final String {{ attribute.name | snake_case | upcase }} = "{{ attribute.name }}";
{%- endif -%}
{%- endfor %}

private Attributes() {
// Hide constructor
}
}
{%- endif -%}
65 changes: 42 additions & 23 deletions lib/templates/android/inc/_attributes_getter_setter.liquid
Original file line number Diff line number Diff line change
@@ -1,39 +1,58 @@
{%- for attribute in entity.attributes %}

{% if attribute.realm_read_only.size == 0 -%}
{%- if attribute.realm_read_only.size == 0 %}
{%- comment %} ******* CONVERT TYPE CAPTURE ******* {% endcomment -%}
{%- capture convert_type -%}

{%- capture visibility %}
{%- if attribute.enum_type.size > 0 and params.hide_members_with_enum.size > 0 -%}
protected
{%- else -%}
public
{%- endif %}
{%- endcapture %}

{%- capture convert_type %}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers == true -%}
{%- if params.use_wrappers.size > 0 and attribute.optional == true -%}
{%- include 'inc/wrapper_type_converter' -%}
{%- else -%}
{%- include 'inc/type_converter' -%}
{%- endif %}
{%- endif %}
{%- endcapture %}

{%- capture isPrimitives %}
{%- include 'inc/primitives' %}
{%- endcapture %}

{%- capture annotation %}
{%- if params.support_annotations.size > 0 %}
{%- if isPrimitives == "false" -%}
{%- if params.use_wrappers.size > 0 or attribute.enum_type.size > 0 or isPrimitives == "false" -%}
@android.support.annotation.{% if attribute.optional == true %}Nullable{% else %}NonNull{%- endif -%}
{%- if attribute.support_annotation.size > 0 %}|{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endcapture -%}
{%- capture isPrimitives -%}
{%- include 'inc/primitives' -%}
{%- endcapture -%}
{%- capture annotation -%}{%- if params.support_annotations == true -%}
{%- if params.use_wrappers == true or attribute.enum_type.size > 0 or isPrimitives == "false" -%}
@android.support.annotation.{% if attribute.optional == true %}Nullable{% else %}NonNull{% endif -%}
{%- if attribute.support_annotation.size > 0 %}|{% endif -%}
{%- endif -%}
{%- if attribute.support_annotation.size > 0 %}@android.support.annotation.{{ attribute.support_annotation }}{%- endif -%}
{%- endif -%}
{%- endcapture -%}
{%- assign name = attribute.name -%}
{%- if annotation.size > 0 %}{{ annotation | replace: "|","
{%- if attribute.support_annotation.size > 0 -%}
@android.support.annotation.{{ attribute.support_annotation }}
{%- endif %}
{%- endif %}
{%- endcapture %}
{%- assign name = attribute.name %}
{{ empty_line }}

{%- if annotation.size > 0 %}
{{ annotation | replace: "|","
" }}
{% endif -%}
public {{ convert_type }} get{{ name | titleize }}() {
{%- endif %}
{{ visibility }} {{ convert_type }} get{{ name | titleize }}() {
return {{ name }};
}

public void set{{ name | titleize }}({%- if annotation.size > 0 %}{{ annotation | replace: "|"," " }} {% endif %}final {{ convert_type }} {{ name }}) {
{{ visibility }} void set{{ name | titleize }}({%- if annotation.size > 0 %}{{ annotation | replace: "|"," " }} {% endif %}final {{ convert_type }} {{ name }}) {
this.{{ name }} = {{ name }};
}
{%- endif -%}
{%- endfor -%}
{%- endif %}
{%- endfor %}
11 changes: 4 additions & 7 deletions lib/templates/android/inc/_attributes_properties.liquid
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

{% for attribute in entity.attributes -%}
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_read_only.size == 0 -%}
{%- comment %} ******* CONVERT TYPE CAPTURE ******* {% endcomment -%}
{%- capture convert_type -%}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers == true -%}
{%- if params.use_wrappers == "true" and attribute.optional == true -%}
{%- include 'inc/wrapper_type_converter' -%}
{%- else -%}
{%- include 'inc/type_converter' -%}
Expand All @@ -21,16 +21,13 @@
{%- if attribute.indexed == true %}
@Index
{%- endif -%}
{%- if attribute.realm_ignored == true or attribute.enum_type.size > 0 %}
{%- if attribute.realm_ignored == true %}
@Ignore
{%- endif -%}
{%- if attribute.enum_type.size > 0 %}
private {{ attribute.enum_type }} {{ name }}Enum;
{%- endif -%}
{%- if attribute.json_key_path.size > 0 %}
@SerializedName("{{ attribute.json_key_path }}")
{%- endif -%}
{%- if params.support_annotations == true and attribute.support_annotation.size > 0 %}
{%- if params.support_annotations.size > 0 and attribute.support_annotation.size > 0 %}
@android.support.annotation.{{ attribute.support_annotation }}
{%- endif %}
private {{ convert_type }} {{ name }};
Expand Down
15 changes: 6 additions & 9 deletions lib/templates/android/inc/_enum_getter_setter.liquid
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
{%- for attribute in entity.attributes -%}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}

{%- if params.support_annotations == true -%}
{%- for attribute in entity.attributes %}

{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 %}
{% if params.support_annotations.size > 0 %}
@android.support.annotation.Nullable
{%- capture annotation -%}@android.support.annotation.NonNull {% endcapture -%}
{%- endif -%}

{%- capture annotation -%}
{%- if params.support_annotations == true -%}
@android.support.annotation.NonNull {% endif -%}
{%- endcapture -%}
{%- assign name = attribute.name %}
{{ annotation }}
public {{ attribute.enum_type }} get{{ name | titleize }}Enum() {
return {{ attribute.enum_type }}.get(get{{ name | titleize }}());
}
Expand All @@ -19,4 +15,5 @@
this.{{ name }} = {{ name }}.getJsonValue();
}
{%- endif -%}

{%- endfor -%}
20 changes: 16 additions & 4 deletions lib/templates/android/inc/_primitives.liquid
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
{%- case attribute.type -%}
{%- when 'integer_16' -%}
{%- capture convert_type %}
{%- if attribute.realm_read_only.size == 0 and attribute.enum_type.size > 0 -%}
String
{%- else -%}
{%- if params.use_wrappers.size > 0 and attribute.optional == true -%}
{%- include 'inc/wrapper_type_converter' -%}
{%- else -%}
{%- include 'inc/type_converter' -%}
{%- endif %}
{%- endif %}
{%- endcapture %}

{%- case convert_type -%}
{%- when 'integer_16' or 'short' -%}
true
{%- when 'integer_32' -%}
{%- when 'integer_32' or 'int' -%}
true
{%- when 'integer_64' -%}
{%- when 'integer_64' or 'long' -%}
true
{%- when 'double' or 'decimal' -%}
true
Expand Down
11 changes: 6 additions & 5 deletions lib/templates/android/inc/_relationships_enum.liquid
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{%- if entity.relationships.size > 0 and entity.has_only_inverse == false %}

public static class Relationships {
private Relationships() {
// Hide constructor
}
{% for relationship in entity.relationships -%}
public static final class Relationships {
{%- for relationship in entity.relationships -%}
{%- if relationship.inverse == false %}
public static final String {{ relationship.name | snake_case | upcase }} = "{{ relationship.name }}";
{%- endif -%}
{%- endfor %}

private Relationships() {
// Hide constructor
}
}
{%- endif -%}
49 changes: 27 additions & 22 deletions lib/templates/android/inc/_relationships_getter_setter.liquid
Original file line number Diff line number Diff line change
@@ -1,42 +1,47 @@
{%- for relationship in entity.relationships %}

{% if relationship.inverse == false -%}
{%- capture relationship_type -%}
{%- if relationship.inverse == false %}
{%- capture relationship_type %}
{%- if relationship.realm_read_only.size == 0 and relationship.enum_type.size > 0 -%}
String
{%- else -%}
{%- if relationship.destination.size == 0 -%}
{%- else %}
{%- if relationship.destination.size == 0 %}
{%- if relationship.type != "to_many" -%}
{{ relationship.inverse_type }}
{%- else -%}
RealmList<{{ relationship.inverse_type }}>
{%- endif -%}
{%- endif %}
{%- else -%}
List<{{ relationship.destination }}>
{%- endif -%}
{%- endif -%}
{%- endcapture -%}
{%- endif %}
{%- endif %}
{%- endcapture %}

{%- capture annotation -%}
{%- if params.support_annotations == true -%}
{%- if relationship.optional == true -%}
@android.support.annotation.Nullable{% endif -%}
{%- if relationship.optional == false -%}
@android.support.annotation.NonNull{% endif -%}
{%- if relationship.support_annotation.size > 0 %}|@android.support.annotation.{{ relationship.support_annotation }}{% endif -%}
{% endif -%}
{%- endcapture -%}
{%- capture annotation %}
{%- if params.support_annotations.size > 0 %}
{%- if relationship.optional == true -%}
@android.support.annotation.Nullable
{%- endif %}
{%- if relationship.optional == false -%}
@android.support.annotation.NonNull
{%- endif %}
{%- if relationship.support_annotation.size > 0 -%}
|@android.support.annotation.{{ relationship.support_annotation }}
{%- endif %}
{%- endif %}
{%- endcapture %}

{%- assign name = relationship.name %}
{%- if annotation.size > 0 %}{{ annotation | replace: "|","
{% comment %} *** Empty line *** {% endcomment %}
{%- if annotation.size > 0 %}
{{ annotation | replace: "|","
" }}
{% endif -%}
{%- endif %}
public {{ relationship_type }} get{{ name | titleize }}() {
return {{ name }};
}

public void set{{ name | titleize }}({%- if annotation.size > 0 %}{{ annotation | replace: "|"," " }} {% endif %}final {{ relationship_type }} {{ name }}) {
this.{{ name }} = {{ name }};
}
{%- endif -%}
{%- endfor -%}
{%- endif %}
{%- endfor %}
Loading