Class: HashObject::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_object.rb

Overview

An internal object that records the state of the mapping between individual keys in a hash object and the actual methods that need to be created.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sym, options) ⇒ Element

Creates an element mapping.

Parameters:

  • sym (Symbol)

    The symbol that defines the method name (and possibly the element string)

  • options (Hash)

    The initialization options

Options Hash (options):

  • :required (Boolean)

    Whether this element is required. Default is true.

  • :default (Object, Proc)

    The default value for the element, if not seen.

  • :type (Class)

    The type of the element to parse it into.

  • :builder (Object)

    If this is a complex object that needs to be constructed, you can pass in a builder object to do the object initialization, circumventing the standard policy.

  • :name (String)

    If you want to map a regular hash key string into a symbol.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/hash_object.rb', line 50

def initialize(sym, options)
  @sym = sym
  @required = options[:required] != false
  @default = options[:default]
  @type = options[:type]
  @single = options[:single]
  @builder = options[:builder]
  @name = options[:name]

  if @type 
    raise ConfigurationError, "'#{sym}' requires a type: #{@type}" unless @type.is_a?(Class)
    if !@type.respond_to?(:parse)
      raise ConfigurationError, "'#{sym}' attribute requires type '#{@type.name}' to implement 'parse'"
    end
  end
end

Instance Attribute Details

#nameSymbol (readonly)

Returns sym The name that is to be parsed from the Hash, if it is not the symbol name.

Returns:

  • (Symbol)

    sym The name that is to be parsed from the Hash, if it is not the symbol name.



38
39
40
# File 'lib/hash_object.rb', line 38

def name
  @name
end

#symSymbol (readonly)

Returns sym The name of the method.

Returns:

  • (Symbol)

    sym The name of the method



36
37
38
# File 'lib/hash_object.rb', line 36

def sym
  @sym
end

Instance Method Details

#default_valueObject

An abstraction around the default value of the object, whether it is a reified object or a Proc that will generate the default value.

Returns:

  • (Object)

    The default object



108
109
110
111
112
113
114
# File 'lib/hash_object.rb', line 108

def default_value
  if @default.is_a?(Proc)
    @default.call
  else
    @default
  end
end

#set(obj, value) ⇒ Object

Sets the value of the newly created element, either parsing the value, setting the default, or using the builder.

Parameters:

  • obj (Object)

    The object being altered.

  • value (Object)

    The value being set on the object.

Returns:

  • (Object)

    The value that is set on the object being created.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/hash_object.rb', line 73

def set(obj, value)
  if @type
    if @single
      value = @type.parse(value)
    else
      value = value.map{|e| @type.parse(e)}
    end
  elsif @builder
    if @single
      value = @builder.call(value)
    else
      value = value.map{|e| @builder.call(e)}
    end
  end
  obj.send("#{@sym}=".to_sym, value)
end

#set_default(obj) ⇒ nil

Sets the default value of the object.

Parameters:

  • obj (Object)

    The object being altered.

Returns:

  • (nil)

Raises:



95
96
97
98
99
100
101
# File 'lib/hash_object.rb', line 95

def set_default(obj)
  if @required
    raise ConfigurationError, "The '#{@sym}' attribute is required for '#{obj.class.name}'"
  else
    obj.send("#{@sym}=".to_sym, default_value)
  end
end