module RandomAttributes
Getters, setters and object building for a someone elses data structure.
Example:
class Runner include RandomAttributes attribute 'runnerName', alias: :name end runner = Runner.new "runnerName" => "Phar Lap" runner.name #=> "Phar Lap"
Constants
- VERSION
Public Class Methods
new(attrs = nil)
click to toggle source
# File lib/random_attributes.rb, line 129 def initialize(attrs = nil) parse attrs end
Public Instance Methods
attributes()
click to toggle source
# File lib/random_attributes.rb, line 149 def attributes @raw_attributes ||= {}.freeze end
cache_key()
click to toggle source
# File lib/random_attributes.rb, line 133 def cache_key @raw_attributes_hash end
parse(attrs = nil)
click to toggle source
# File lib/random_attributes.rb, line 137 def parse(attrs = nil) @parsed_attributes = {} attrs && attrs.stringify_keys! run_callbacks :parse do @raw_attributes = attributes.merge(attrs.nil? ? {} : attrs).freeze @raw_attributes_hash = Digest::MD5.hexdigest(attributes.to_s) end self end
Also aliased as: merge_attributes
Private Instance Methods
_get_attribute(register)
click to toggle source
# File lib/random_attributes.rb, line 178 def _get_attribute(register) value = attributes[register.aliased] unless value if register.try_node if try_node = send(register.try_node) value = _search_originals register, try_node else value = _search_originals register, attributes end elsif register.parent_node if parent_node = send(register.parent_node) value = _search_originals register, parent_node end else value = _search_originals register, attributes end end register.cast(value) end
_search_originals(register, node, value = nil)
click to toggle source
# File lib/random_attributes.rb, line 171 def _search_originals(register, node, value = nil) register.originals.each do |key| break if value = node[key] end value end
get_attribute(register)
click to toggle source
# File lib/random_attributes.rb, line 159 def get_attribute(register) if parsed_attributes.key?(register.aliased) parsed_attributes[register.aliased] else parsed_attributes[register.aliased] = _get_attribute(register) end end
parsed_attributes()
click to toggle source
# File lib/random_attributes.rb, line 155 def parsed_attributes @parsed_attributes ||= {} end
set_attribute(key, value)
click to toggle source
# File lib/random_attributes.rb, line 167 def set_attribute(key, value) parsed_attributes[key.to_s] = value end