Class: SettingsDB::Generators::GeneratedAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/settingsdb/generators/generated_attribute.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, attr_options={})
  @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   = attr_options
end

Instance Attribute Details

#attr_optionsObject (readonly)

Returns the value of attribute attr_options.



9
10
11
# File 'lib/settingsdb/generators/generated_attribute.rb', line 9

def attr_options
  @attr_options
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/settingsdb/generators/generated_attribute.rb', line 8

def name
  @name
end

#typeObject

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, attr_options = *parse_type_and_options(type)
  new(name, type, has_index, attr_options)
end

Instance Method Details

#defaultObject



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_typeObject



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

Returns:

  • (Boolean)


88
89
90
# File 'lib/settingsdb/generators/generated_attribute.rb', line 88

def has_index?
  @has_index
end

#has_uniq_index?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/settingsdb/generators/generated_attribute.rb', line 92

def has_uniq_index?
  @has_uniq_index
end

#human_nameObject



76
77
78
# File 'lib/settingsdb/generators/generated_attribute.rb', line 76

def human_name
  name.to_s.humanize
end

#index_nameObject



80
81
82
# File 'lib/settingsdb/generators/generated_attribute.rb', line 80

def index_name
  reference? ? "#{name}_id" : name
end

#inject_index_optionsObject



100
101
102
# File 'lib/settingsdb/generators/generated_attribute.rb', line 100

def inject_index_options
  has_uniq_index? ? ", :unique => true" : ''
end

#inject_optionsObject



96
97
98
# File 'lib/settingsdb/generators/generated_attribute.rb', line 96

def inject_options
  @attr_options.blank? ? '' : ", #{@attr_options.to_s.gsub(/[{}]/, '')}"
end

#reference?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/settingsdb/generators/generated_attribute.rb', line 84

def reference?
  self.type.in?([:references, :belongs_to])
end