Class: Napa::CLI::GeneratedAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/napa/cli/migration.rb

Overview

Constant Summary collapse

INDEX_OPTIONS =

:nodoc:

%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 = {}) ⇒ GeneratedAttribute

Returns a new instance of GeneratedAttribute.



132
133
134
135
136
137
138
# File 'lib/napa/cli/migration.rb', line 132

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.



87
88
89
# File 'lib/napa/cli/migration.rb', line 87

def attr_options
  @attr_options
end

#index_nameObject



182
183
184
185
186
187
188
# File 'lib/napa/cli/migration.rb', line 182

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.



86
87
88
# File 'lib/napa/cli/migration.rb', line 86

def name
  @name
end

#typeObject

Returns the value of attribute type.



86
87
88
# File 'lib/napa/cli/migration.rb', line 86

def type
  @type
end

Class Method Details

.parse(column_definition) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/napa/cli/migration.rb', line 91

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)


110
111
112
# File 'lib/napa/cli/migration.rb', line 110

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

Instance Method Details

#column_nameObject



190
191
192
# File 'lib/napa/cli/migration.rb', line 190

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

#defaultObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/napa/cli/migration.rb', line 154

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



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/napa/cli/migration.rb', line 140

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)


194
195
196
# File 'lib/napa/cli/migration.rb', line 194

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

#has_index?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/napa/cli/migration.rb', line 206

def has_index?
  @has_index
end

#has_uniq_index?Boolean

Returns:

  • (Boolean)


210
211
212
# File 'lib/napa/cli/migration.rb', line 210

def has_uniq_index?
  @has_uniq_index
end

#human_nameObject



178
179
180
# File 'lib/napa/cli/migration.rb', line 178

def human_name
  name.humanize
end

#inject_index_optionsObject



222
223
224
# File 'lib/napa/cli/migration.rb', line 222

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

#inject_optionsObject



218
219
220
# File 'lib/napa/cli/migration.rb', line 218

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

#password_digest?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'lib/napa/cli/migration.rb', line 214

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

#plural_nameObject



170
171
172
# File 'lib/napa/cli/migration.rb', line 170

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

#polymorphic?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/napa/cli/migration.rb', line 202

def polymorphic?
  self.attr_options.has_key?(:polymorphic)
end

#reference?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/napa/cli/migration.rb', line 198

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

#singular_nameObject



174
175
176
# File 'lib/napa/cli/migration.rb', line 174

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