Class: Napa::CLI::GeneratedAttribute
- Inherits:
-
Object
- Object
- Napa::CLI::GeneratedAttribute
- Defined in:
- lib/napa/cli/generated_attribute.rb
Overview
directly ported with slight modification from github.com/rails/rails/blob/76883f92374c6395f13c16628e1d87d40b6d2399/railties/lib/rails/generators/generated_attribute.rb
Constant Summary collapse
- INDEX_OPTIONS =
:nodoc:
%w(index uniq)
- UNIQ_INDEX_OPTIONS =
%w(uniq)
Instance Attribute Summary collapse
-
#attr_options ⇒ Object
readonly
Returns the value of attribute attr_options.
- #index_name ⇒ Object
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #column_name ⇒ Object
- #default ⇒ Object
- #factory_stub ⇒ Object
- #field_type ⇒ Object
- #foreign_key? ⇒ Boolean
- #has_index? ⇒ Boolean
- #has_uniq_index? ⇒ Boolean
- #human_name ⇒ Object
-
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
constructor
A new instance of GeneratedAttribute.
- #inject_index_options ⇒ Object
- #inject_options ⇒ Object
- #password_digest? ⇒ Boolean
- #plural_name ⇒ Object
- #polymorphic? ⇒ Boolean
- #reference? ⇒ Boolean
- #required? ⇒ Boolean
- #singular_name ⇒ Object
Constructor Details
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
Returns a new instance of GeneratedAttribute.
55 56 57 58 59 60 61 |
# File 'lib/napa/cli/generated_attribute.rb', line 55 def initialize(name, type=nil, index_type=false, ={}) @name = name @type = type || :string @has_index = INDEX_OPTIONS.include?(index_type) @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type) @attr_options = end |
Instance Attribute Details
#attr_options ⇒ Object (readonly)
Returns the value of attribute attr_options.
10 11 12 |
# File 'lib/napa/cli/generated_attribute.rb', line 10 def @attr_options end |
#index_name ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/napa/cli/generated_attribute.rb', line 121 def index_name @index_name ||= if polymorphic? %w(id type).map { |t| "#{name}_#{t}" } else column_name end end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/napa/cli/generated_attribute.rb', line 9 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
9 10 11 |
# File 'lib/napa/cli/generated_attribute.rb', line 9 def type @type end |
Class Method Details
.parse(column_definition) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/napa/cli/generated_attribute.rb', line 14 def parse(column_definition) name, type, has_index = column_definition.split(':') # if user provided "name:index" instead of "name:string:index" # type should be set blank so GeneratedAttribute's constructor # could set it to :string has_index, type = type, nil if INDEX_OPTIONS.include?(type) type, = *(type) type = type.to_sym if type if type && reference?(type) references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { unique: true } : true [:index] = references_index end new(name, type, has_index, ) end |
.reference?(type) ⇒ Boolean
33 34 35 |
# File 'lib/napa/cli/generated_attribute.rb', line 33 def reference?(type) [:references, :belongs_to].include? type end |
Instance Method Details
#column_name ⇒ Object
129 130 131 |
# File 'lib/napa/cli/generated_attribute.rb', line 129 def column_name @column_name ||= reference? ? "#{name}_id" : name end |
#default ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/napa/cli/generated_attribute.rb', line 77 def default @default ||= case type when :integer then 1 when :float then 1.5 when :decimal then "9.99" when :datetime, :timestamp, :time then Time.now.to_s(:db) when :date then Date.today.to_s(:db) when :string then name == "type" ? "" : "MyString" when :text then "MyText" when :boolean then false when :references, :belongs_to then nil else "" end end |
#factory_stub ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/napa/cli/generated_attribute.rb', line 93 def factory_stub case type when :integer then "1" when :float then "1.5" when :decimal then "9.99" when :datetime, :timestamp, :time then "{ Time.now }" when :date then "{ Date.today }" when :string then '"MyString"' when :text then '"MyText"' when :boolean then "false" when :digest then '"password"' else '"Unknown"' end end |
#field_type ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/napa/cli/generated_attribute.rb', line 63 def field_type @field_type ||= case type when :integer then :number_field when :float, :decimal then :text_field when :time then :time_select when :datetime, :timestamp then :datetime_select when :date then :date_select when :text then :text_area when :boolean then :check_box else :text_field end end |
#foreign_key? ⇒ Boolean
133 134 135 |
# File 'lib/napa/cli/generated_attribute.rb', line 133 def foreign_key? !!(name =~ /_id$/) end |
#has_index? ⇒ Boolean
149 150 151 |
# File 'lib/napa/cli/generated_attribute.rb', line 149 def has_index? @has_index end |
#has_uniq_index? ⇒ Boolean
153 154 155 |
# File 'lib/napa/cli/generated_attribute.rb', line 153 def has_uniq_index? @has_uniq_index end |
#human_name ⇒ Object
117 118 119 |
# File 'lib/napa/cli/generated_attribute.rb', line 117 def human_name name.humanize end |
#inject_index_options ⇒ Object
165 166 167 |
# File 'lib/napa/cli/generated_attribute.rb', line 165 def has_uniq_index? ? ", unique: true" : "" end |
#inject_options ⇒ Object
161 162 163 |
# File 'lib/napa/cli/generated_attribute.rb', line 161 def "".tap { |s| @attr_options.each { |k,v| s << ", #{k}: #{v.inspect}" } } end |
#password_digest? ⇒ Boolean
157 158 159 |
# File 'lib/napa/cli/generated_attribute.rb', line 157 def password_digest? name == 'password' && type == :digest end |
#plural_name ⇒ Object
109 110 111 |
# File 'lib/napa/cli/generated_attribute.rb', line 109 def plural_name name.sub(/_id$/, '').pluralize end |
#polymorphic? ⇒ Boolean
141 142 143 |
# File 'lib/napa/cli/generated_attribute.rb', line 141 def polymorphic? self..has_key?(:polymorphic) end |
#reference? ⇒ Boolean
137 138 139 |
# File 'lib/napa/cli/generated_attribute.rb', line 137 def reference? self.class.reference?(type) end |
#required? ⇒ Boolean
145 146 147 |
# File 'lib/napa/cli/generated_attribute.rb', line 145 def required? self.[:required] end |
#singular_name ⇒ Object
113 114 115 |
# File 'lib/napa/cli/generated_attribute.rb', line 113 def singular_name name.sub(/_id$/, '').singularize end |