class AwsRecord::Generators::GeneratedAttribute

Constants

INVALID_HKEY_TYPES
OPTS

Attributes

name[R]
options[RW]
type[R]

Public Class Methods

new(name, type = :string_attr, options = {}) click to toggle source
# 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
parse(field_definition) click to toggle source
# 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

Private Class Methods

parse_option(name, opt) click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 60
def parse_option(name, opt)
  case opt

  when 'hkey'
    [:hash_key, true]
  when 'rkey'
    [:range_key, true]
  when 'persist_nil'
    [:persist_nil, true]
  when /db_attr_name\{(\w+)\}/
    [:database_attribute_name, "\"#{::Regexp.last_match(1)}\""]
  when /ddb_type\{(S|N|B|BOOL|SS|NS|BS|M|L)\}/i
    [:dynamodb_type, "\"#{::Regexp.last_match(1).upcase}\""]
  when /default_value\{(.+)\}/
    [:default_value, ::Regexp.last_match(1)]
  else
    raise ArgumentError, "You provided an invalid option for #{name}: #{opt}"
  end
end
parse_type(name, type) click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 80
def parse_type(name, type)
  case type.downcase

  when 'bool', 'boolean'
    :boolean_attr
  when 'date'
    :date_attr
  when 'datetime'
    :datetime_attr
  when 'float'
    :float_attr
  when 'int', 'integer'
    :integer_attr
  when 'list'
    :list_attr
  when 'map'
    :map_attr
  when 'num_set', 'numeric_set', 'nset'
    :numeric_set_attr
  when 'string_set', 's_set', 'sset'
    :string_set_attr
  when 'string'
    :string_attr
  else
    raise ArgumentError, "Invalid type for #{name}: #{type}"
  end
end
parse_type_and_options(name, type, opts) click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 55
def parse_type_and_options(name, type, opts)
  opts ||= []
  [parse_type(name, type), opts.to_h { |opt| parse_option(name, opt) }]
end
validate_opt_combs(name, type, opts) click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 39
def validate_opt_combs(name, type, opts)
  return unless opts

  is_hkey = opts.key?(:hash_key)
  is_rkey = opts.key?(:range_key)

  if is_hkey && is_rkey
    raise ArgumentError,
          "Field #{name} cannot be a range key and hash key simultaneously"
  end
  return unless is_hkey && INVALID_HKEY_TYPES.include?(type)

  raise ArgumentError,
        "Field #{name} cannot be a hash key and be of type #{type}"
end

Public Instance Methods

column_name() click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 125
def column_name
  if @name == 'password_digest'
    'password'
  else
    @name
  end
end
field_type() click to toggle source
# 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_name() click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 133
def human_name
  name.humanize
end
password_digest?() click to toggle source

Methods used by rails scaffolding

# File lib/generators/aws_record/generated_attribute.rb, line 117
def password_digest?
  @digest
end
polymorphic?() click to toggle source
# File lib/generators/aws_record/generated_attribute.rb, line 121
def polymorphic?
  false
end