Class: Hashblock::BlockEvaluator

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

Defined Under Namespace

Classes: DuplicateProperty

Constant Summary collapse

MERGE_STRATEGIES =
[:array, :exception, :first_wins, :last_wins]

Instance Method Summary collapse

Constructor Details

#initialize(block, merge_strategy) ⇒ BlockEvaluator

Returns a new instance of BlockEvaluator.



31
32
33
34
35
36
37
38
39
40
# File 'lib/hashblock/block_evaluator.rb', line 31

def initialize(block, merge_strategy)
  unless block.is_a?(Proc)
    raise ArgumentError, "Must supply a Proc, not a `#{block.class}'"
  end
  
  @merge_strategy = merge_strategy
  @properties = {}
  
  self.instance_eval(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/hashblock/block_evaluator.rb', line 6

def method_missing(sym, *args, &block)
  #
  # Setter method, looks like one of the following:
  #
  # some_property value
  # self.some_property = value
  if args.size == 1
    if block_given?
      raise ArgumentError, "Setters may not have blocks supplied"
    end
    __set(sym.to_s.chomp("="), args.first)
  
  #
  # Nested block, convert this to a hash and graft it on to self.  Looks
  # like this:
  #
  # some_property do
  #   some_nested_property true
  # end
  elsif args.empty? and block_given?
    inner = self.class.new(block, @merge_strategy)
    __set(sym, inner.to_hash)
  end
end

Instance Method Details

#to_hashObject



42
43
44
# File 'lib/hashblock/block_evaluator.rb', line 42

def to_hash
  @properties
end