Class: Lanes::Command::ModelAttribute
- Inherits:
-
Object
- Object
- Lanes::Command::ModelAttribute
- Defined in:
- lib/lanes/command/model_attribute.rb
Overview
:nodoc:
Constant Summary collapse
- INDEX_OPTIONS =
%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
- #belongs_to? ⇒ Boolean
- #client_type ⇒ Object
- #column_name ⇒ Object
- #default ⇒ 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 = {}) ⇒ ModelAttribute
constructor
A new instance of ModelAttribute.
- #inject_index_options ⇒ Object
- #inject_options ⇒ Object
- #options_for_migration ⇒ 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 = {}) ⇒ ModelAttribute
Returns a new instance of ModelAttribute.
60 61 62 63 64 65 66 |
# File 'lib/lanes/command/model_attribute.rb', line 60 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.
12 13 14 |
# File 'lib/lanes/command/model_attribute.rb', line 12 def @attr_options end |
#index_name ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/lanes/command/model_attribute.rb', line 110 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.
11 12 13 |
# File 'lib/lanes/command/model_attribute.rb', line 11 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
11 12 13 |
# File 'lib/lanes/command/model_attribute.rb', line 11 def type @type end |
Class Method Details
.parse(column_definition) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lanes/command/model_attribute.rb', line 16 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
35 36 37 |
# File 'lib/lanes/command/model_attribute.rb', line 35 def reference?(type) [:references, :belongs_to].include? type end |
Instance Method Details
#belongs_to? ⇒ Boolean
182 183 184 |
# File 'lib/lanes/command/model_attribute.rb', line 182 def belongs_to? :belongs_to == type end |
#client_type ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/lanes/command/model_attribute.rb', line 167 def client_type @client_type ||= case type when :datetime then 'date' when :integer then 'integer' when :float then 'number' when :text then 'string' when :string then 'string' when :boolean then 'boolean' when :decimal then 'bigdec' when :hstore then 'object' when :references, :belongs_to then 'integer' else 'any' end end |
#column_name ⇒ Object
118 119 120 |
# File 'lib/lanes/command/model_attribute.rb', line 118 def column_name @column_name ||= reference? ? "#{name}_id" : name end |
#default ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/lanes/command/model_attribute.rb', line 82 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 |
#field_type ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/lanes/command/model_attribute.rb', line 68 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
122 123 124 |
# File 'lib/lanes/command/model_attribute.rb', line 122 def foreign_key? !!(name =~ /_id$/) end |
#has_index? ⇒ Boolean
138 139 140 |
# File 'lib/lanes/command/model_attribute.rb', line 138 def has_index? @has_index end |
#has_uniq_index? ⇒ Boolean
142 143 144 |
# File 'lib/lanes/command/model_attribute.rb', line 142 def has_uniq_index? @has_uniq_index end |
#human_name ⇒ Object
106 107 108 |
# File 'lib/lanes/command/model_attribute.rb', line 106 def human_name name.humanize end |
#inject_index_options ⇒ Object
154 155 156 |
# File 'lib/lanes/command/model_attribute.rb', line 154 def has_uniq_index? ? ", unique: true" : "" end |
#inject_options ⇒ Object
150 151 152 |
# File 'lib/lanes/command/model_attribute.rb', line 150 def "".tap { |s| .each { |k,v| s << ", #{k}: #{v.inspect}" } } end |
#options_for_migration ⇒ Object
158 159 160 161 162 163 164 165 |
# File 'lib/lanes/command/model_attribute.rb', line 158 def @attr_options.dup.tap do || if required? .delete(:required) [:null] = false end end end |
#password_digest? ⇒ Boolean
146 147 148 |
# File 'lib/lanes/command/model_attribute.rb', line 146 def password_digest? name == 'password' && type == :digest end |
#plural_name ⇒ Object
98 99 100 |
# File 'lib/lanes/command/model_attribute.rb', line 98 def plural_name name.sub(/_id$/, '').pluralize end |
#polymorphic? ⇒ Boolean
130 131 132 |
# File 'lib/lanes/command/model_attribute.rb', line 130 def polymorphic? self.[:polymorphic] end |
#reference? ⇒ Boolean
126 127 128 |
# File 'lib/lanes/command/model_attribute.rb', line 126 def reference? self.class.reference?(type) end |
#required? ⇒ Boolean
134 135 136 |
# File 'lib/lanes/command/model_attribute.rb', line 134 def required? self.[:required] end |
#singular_name ⇒ Object
102 103 104 |
# File 'lib/lanes/command/model_attribute.rb', line 102 def singular_name name.sub(/_id$/, '').singularize end |