Class: SettingsDB::Generators::GeneratedAttribute
- Inherits:
-
Object
- Object
- SettingsDB::Generators::GeneratedAttribute
- Defined in:
- lib/settingsdb/generators/generated_attribute.rb
Instance Attribute Summary collapse
-
#attr_options ⇒ Object
readonly
Returns the value of attribute attr_options.
-
#name ⇒ Object
Returns the value of attribute name.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #default ⇒ Object
- #field_type ⇒ Object
- #has_index? ⇒ Boolean
- #has_uniq_index? ⇒ Boolean
- #human_name ⇒ Object
- #index_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
- #reference? ⇒ Boolean
Constructor Details
#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ GeneratedAttribute
Returns a new instance of GeneratedAttribute.
40 41 42 43 44 45 46 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 40 def initialize(name, type=nil, index_type=false, ={}) @name = name @type = (type.presence || :string).to_sym @has_index = %w(index uniq).include?(index_type) @has_uniq_index = %w(uniq).include?(index_type) @attr_options = end |
Instance Attribute Details
#attr_options ⇒ Object (readonly)
Returns the value of attribute attr_options.
9 10 11 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 9 def @attr_options end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 8 def name @name end |
#type ⇒ Object
Returns the value of attribute type.
8 9 10 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 8 def type @type end |
Class Method Details
.parse(column_definition) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 12 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 %w(index uniq).include?(type) type, = *(type) new(name, type, has_index, ) end |
Instance Method Details
#default ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 61 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
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 48 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 |
#has_index? ⇒ Boolean
88 89 90 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 88 def has_index? @has_index end |
#has_uniq_index? ⇒ Boolean
92 93 94 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 92 def has_uniq_index? @has_uniq_index end |
#human_name ⇒ Object
76 77 78 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 76 def human_name name.to_s.humanize end |
#index_name ⇒ Object
80 81 82 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 80 def index_name reference? ? "#{name}_id" : name end |
#inject_index_options ⇒ Object
100 101 102 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 100 def has_uniq_index? ? ", :unique => true" : '' end |
#inject_options ⇒ Object
96 97 98 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 96 def @attr_options.blank? ? '' : ", #{@attr_options.to_s.gsub(/[{}]/, '')}" end |
#reference? ⇒ Boolean
84 85 86 |
# File 'lib/settingsdb/generators/generated_attribute.rb', line 84 def reference? self.type.in?([:references, :belongs_to]) end |