Class: Hippo::Command::ModelAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/command/model_attribute.rb

Overview

:nodoc:

Constant Summary collapse

INDEX_OPTIONS =
%w(index uniq)
UNIQ_INDEX_OPTIONS =
%w(uniq)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil, index_type = false, attr_options = {}) ⇒ ModelAttribute

Returns a new instance of ModelAttribute.



61
62
63
64
65
66
67
# File 'lib/hippo/command/model_attribute.rb', line 61

def initialize(name, type=nil, index_type=false, attr_options={})
  @name           = name
  @type           = type || :string
  @has_index      = INDEX_OPTIONS.include?(index_type)
  @has_uniq_index = UNIQ_INDEX_OPTIONS.include?(index_type)
  @attr_options   = attr_options
end

Instance Attribute Details

#attr_optionsObject (readonly)

Returns the value of attribute attr_options.



12
13
14
# File 'lib/hippo/command/model_attribute.rb', line 12

def attr_options
  @attr_options
end

#index_nameObject



115
116
117
118
119
120
121
# File 'lib/hippo/command/model_attribute.rb', line 115

def index_name
  @index_name ||= if polymorphic?
    %w(id type).map { |t| "#{name}_#{t}" }
  else
    column_name
  end
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/hippo/command/model_attribute.rb', line 11

def name
  @name
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'lib/hippo/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
34
# File 'lib/hippo/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, attr_options = *parse_type_and_options(type)
  type = type.to_sym if type

  if type && reference?(type)
    references_index = UNIQ_INDEX_OPTIONS.include?(has_index) ? { unique: true } : true
    attr_options[:index] = references_index
  end

  new(name, type, has_index, attr_options)
end

.reference?(type) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/hippo/command/model_attribute.rb', line 36

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

Instance Method Details

#belongs_to?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/hippo/command/model_attribute.rb', line 187

def belongs_to?
    :belongs_to == type
end

#client_typeObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/hippo/command/model_attribute.rb', line 172

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_nameObject



123
124
125
# File 'lib/hippo/command/model_attribute.rb', line 123

def column_name
  @column_name ||= reference? ? "#{name}_id" : name
end

#decoratorObject



69
70
71
# File 'lib/hippo/command/model_attribute.rb', line 69

def decorator
    name == 'id' ? 'identifier' : 'field'
end

#defaultObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hippo/command/model_attribute.rb', line 87

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



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hippo/command/model_attribute.rb', line 73

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

Returns:

  • (Boolean)


127
128
129
# File 'lib/hippo/command/model_attribute.rb', line 127

def foreign_key?
  !!(name =~ /_id$/)
end

#has_index?Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/hippo/command/model_attribute.rb', line 143

def has_index?
  @has_index
end

#has_uniq_index?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'lib/hippo/command/model_attribute.rb', line 147

def has_uniq_index?
  @has_uniq_index
end

#human_nameObject



111
112
113
# File 'lib/hippo/command/model_attribute.rb', line 111

def human_name
  name.humanize
end

#inject_index_optionsObject



159
160
161
# File 'lib/hippo/command/model_attribute.rb', line 159

def inject_index_options
  has_uniq_index? ? ", unique: true" : ""
end

#inject_optionsObject



155
156
157
# File 'lib/hippo/command/model_attribute.rb', line 155

def inject_options
  "".tap { |s| options_for_migration.each { |k,v| s << ", #{k}: #{v.inspect}" } }
end

#options_for_migrationObject



163
164
165
166
167
168
169
170
# File 'lib/hippo/command/model_attribute.rb', line 163

def options_for_migration
  @attr_options.dup.tap do |options|
    if required?
      options.delete(:required)
      options[:null] = false
    end
  end
end

#password_digest?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/hippo/command/model_attribute.rb', line 151

def password_digest?
  name == 'password' && type == :digest
end

#plural_nameObject



103
104
105
# File 'lib/hippo/command/model_attribute.rb', line 103

def plural_name
  name.sub(/_id$/, '').pluralize
end

#polymorphic?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/hippo/command/model_attribute.rb', line 135

def polymorphic?
  self.attr_options[:polymorphic]
end

#reference?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/hippo/command/model_attribute.rb', line 131

def reference?
  self.class.reference?(type)
end

#required?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/hippo/command/model_attribute.rb', line 139

def required?
  self.attr_options[:required]
end

#singular_nameObject



107
108
109
# File 'lib/hippo/command/model_attribute.rb', line 107

def singular_name
  name.sub(/_id$/, '').singularize
end