Class: HashDealer

Inherits:
Object show all
Defined in:
lib/hash.rb,
lib/hash_dealer.rb

Defined Under Namespace

Classes: Hash

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ HashDealer

initializer just calls the block from within our DSL


33
34
35
36
37
# File 'lib/hash_dealer.rb', line 33

def initialize(opts = {}, &block)
  @parent = opts[:parent]
  @optional_attributes = []
  instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (protected)

method missing


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/hash_dealer.rb', line 72

def method_missing(meth, *args, &block)

  unless args.length > 0 || block_given?
    raise Exception.new(
      "Please provide either a String or a block to #{meth}"
    )
  end
  # a second arg is the options hash
  opts = args[1] || {}
  
  # the value is the first arg
  value = args[0]
  
  if opts[:optional]
    @optional_attributes << meth.to_sym
  end

  @attributes ||= Hash.new
  
  if block_given?
    @attributes[meth.to_sym] = block
  else
    @attributes[meth.to_sym] = value
  end
end

Instance Attribute Details

#optional_attributesObject

Returns the value of attribute optional_attributes


13
14
15
# File 'lib/hash_dealer.rb', line 13

def optional_attributes
  @optional_attributes
end

#parentObject

Returns the value of attribute parent


12
13
14
# File 'lib/hash_dealer.rb', line 12

def parent
  @parent
end

Class Method Details

.define(name, opts = {}, &block) ⇒ Object

define a method of the request factory


21
22
23
# File 'lib/hash_dealer.rb', line 21

def self.define(name, opts = {},  &block)
  self.hashes[name] = [opts, block]
end

.hashesObject

cattr_accessor


16
17
18
# File 'lib/hash_dealer.rb', line 16

def self.hashes
  @hashes ||= {}
end

.roll(name, overrides = {}) ⇒ Object

Raises:

  • (Exception)

25
26
27
28
29
30
# File 'lib/hash_dealer.rb', line 25

def self.roll(name, overrides = {})
  raise Exception.new("No HashDealer called #{name}") unless self.hashes[name]
  self.hashes[name] = self.new(self.hashes[name][0], &self.hashes[name][1]) unless self.hashes[name].is_a?(HashDealer)

  self.hashes[name]._attributes(overrides)
end

Instance Method Details

#[](val) ⇒ Object


44
45
46
# File 'lib/hash_dealer.rb', line 44

def [](val)
  @attributes[val]
end

#_attributes(overrides) ⇒ Object

get the stored attributes for this HashDealer


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

def _attributes(overrides)
  # allows us to set a root value
  return @attributes.clone unless @attributes.is_a?(Hash)

  if @parent.present?
    att = HashDealer.roll(@parent.to_sym)
  else
    att = HashDealer::Hash.new(self.optional_attributes)
  end

  @attributes.each do |k,v|
    att[k] = v.is_a?(Proc) ? v.call(att.merge(overrides)) : v
  end
  # if we have a hash as the first arg, it would override the attributes
  overrides.each_pair do |k,v|
    att[k.to_sym] = v if att.has_key?(k.to_sym)
  end
  att
end

#root(value) ⇒ Object

set the value as the root element for attributes


40
41
42
# File 'lib/hash_dealer.rb', line 40

def root(value)
  @attributes = value
end