class HashCast::AttributesParser::DSL

Attributes

attributes[R]

Public Class Methods

new() click to toggle source
# File lib/hashcast/attributes_parser.rb, line 18
def initialize
  @attributes = []
end

Public Instance Methods

hash(*args, &block) click to toggle source

Redefined becase each class has the built in hash method

# File lib/hashcast/attributes_parser.rb, line 23
def hash(*args, &block)
  method_missing(:hash, *args, &block)
end
method_missing(caster_name, *args, &block) click to toggle source
# File lib/hashcast/attributes_parser.rb, line 27
def method_missing(caster_name, *args, &block)
  attr_name = args[0]
  options   = args[1] || {}
  caster = HashCast.casters[caster_name]

  check_caster_exists!(caster, caster_name)
  check_attr_name_valid!(attr_name)
  check_options_is_hash!(options)

  attribute = HashCast::Metadata::Attribute.new(attr_name, caster, options)
  if block_given?
    attribute.children = HashCast::AttributesParser.parse(&block)
  end
  attributes << attribute
end

Private Instance Methods

check_attr_name_valid!(attr_name) click to toggle source
# File lib/hashcast/attributes_parser.rb, line 51
def check_attr_name_valid!(attr_name)
  if !attr_name.is_a?(Symbol) && !attr_name.is_a?(String)
    raise HashCast::Errors::ArgumentError, "attribute name should be a symbol or string"
  end
end
check_caster_exists!(caster, caster_name) click to toggle source
# File lib/hashcast/attributes_parser.rb, line 45
def check_caster_exists!(caster, caster_name)
  if !caster
    raise HashCast::Errors::CasterNotFoundError, "caster with name '#{caster_name}' is not found"
  end
end
check_options_is_hash!(options) click to toggle source
# File lib/hashcast/attributes_parser.rb, line 57
def check_options_is_hash!(options)
  if !options.is_a?(Hash)
    raise HashCast::Errors::ArgumentError, "attribute options should be a Hash"
  end
end