Class: DiviningRod::Mappings

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, default_opts = {}) ⇒ Mappings

Returns a new instance of Mappings.



22
23
24
25
# File 'lib/divining_rod/mappings.rb', line 22

def initialize(parent, default_opts = {})
  @parent = parent
  @default_opts = Mash.new(default_opts)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/divining_rod/mappings.rb', line 46

def method_missing(meth, *args, &blk)
  # Lets us use map.ua instead of map.pattern :ua
  if Matchers.respond_to?(meth.to_sym)
    opts = args[1,] || {}
    self.pattern(meth, args[0], opts, &blk)
  else
    super
  end
end

Class Attribute Details

.root_definitionObject

Returns the value of attribute root_definition.



6
7
8
# File 'lib/divining_rod/mappings.rb', line 6

def root_definition
  @root_definition
end

Instance Attribute Details

#default_optsObject (readonly)

Returns the value of attribute default_opts.



20
21
22
# File 'lib/divining_rod/mappings.rb', line 20

def default_opts
  @default_opts
end

#parentObject (readonly)

Returns the value of attribute parent.



20
21
22
# File 'lib/divining_rod/mappings.rb', line 20

def parent
  @parent
end

Class Method Details

.define(opts = {}) {|Mappings.new(@root_definition)| ... } ⇒ Object

Yields:



8
9
10
11
12
# File 'lib/divining_rod/mappings.rb', line 8

def define(opts = {})
  @root_definition = Definition.new(opts) { true }
  yield Mappings.new(@root_definition)
  @root_definition
end

.evaluate(obj) ⇒ Object



14
15
16
# File 'lib/divining_rod/mappings.rb', line 14

def evaluate(obj)
  @root_definition.evaluate(obj)
end

Instance Method Details

#default(opts = {}) ⇒ Object



36
37
38
39
40
# File 'lib/divining_rod/mappings.rb', line 36

def default(opts = {})
  definition = Definition.new(merged_opts(opts)) { true }
  append_to_parent(definition)
  definition
end

#pattern(type, pattern, opts = {}) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/divining_rod/mappings.rb', line 27

def pattern(type, pattern, opts = {})
  definition = Matchers.send(type.to_sym, pattern, merged_opts(opts))
  append_to_parent(definition)
  if block_given?
    yield self.class.new(definition)
  end
  definition
end

#with_options(opts) {|self.class.new(parent, opts)| ... } ⇒ Object

Yields:

  • (self.class.new(parent, opts))


42
43
44
# File 'lib/divining_rod/mappings.rb', line 42

def with_options(opts)
  yield self.class.new(parent, opts)
end