Class: AwsRecord::Generators::GeneratedAttribute

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

Constant Summary collapse

OPTS =
%w[hkey rkey persist_nil db_attr_name ddb_type default_value].freeze
INVALID_HKEY_TYPES =
%i[map_attr list_attr numeric_set_attr string_set_attr].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = :string_attr, options = {}) ⇒ GeneratedAttribute

Returns a new instance of GeneratedAttribute.



109
110
111
112
113
114
# File 'lib/generators/aws_record/generated_attribute.rb', line 109

def initialize(name, type = :string_attr, options = {})
  @name = name
  @type = type
  @options = options
  @digest = options.delete(:digest)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



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

def type
  @type
end

Class Method Details

.parse(field_definition) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/aws_record/generated_attribute.rb', line 22

def parse(field_definition)
  name, type, opts = field_definition.split(':')
  type ||= 'string'
  if OPTS.any? { |opt| type.include? opt }
    opts = type
    type = 'string'
  end

  opts = opts.split(',') if opts
  type, opts = parse_type_and_options(name, type, opts)
  validate_opt_combs(name, type, opts)

  new(name, type, opts)
end

Instance Method Details

#column_nameObject



125
126
127
128
129
130
131
# File 'lib/generators/aws_record/generated_attribute.rb', line 125

def column_name
  if @name == 'password_digest'
    'password'
  else
    @name
  end
end

#field_typeObject



11
12
13
14
15
16
17
18
19
# File 'lib/generators/aws_record/generated_attribute.rb', line 11

def field_type
  case @type
  when :integer_attr then :number_field
  when :date_attr then :date_select
  when :datetime_attr then :datetime_select
  when :boolean_attr then :check_box
  else :text_field
  end
end

#human_nameObject



133
134
135
# File 'lib/generators/aws_record/generated_attribute.rb', line 133

def human_name
  name.humanize
end

#password_digest?Boolean

Methods used by rails scaffolding

Returns:

  • (Boolean)


117
118
119
# File 'lib/generators/aws_record/generated_attribute.rb', line 117

def password_digest?
  @digest
end

#polymorphic?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/generators/aws_record/generated_attribute.rb', line 121

def polymorphic?
  false
end