Class: Sergio::Config

Inherits:
Object
  • Object
show all
Includes:
HashMethods
Defined in:
lib/sergio/sergio_config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HashMethods

#hash_from_path, #hash_recursive_append, #hash_recursive_merge_to_arrays, #value_at_path

Constructor Details

#initializeConfig

Returns a new instance of Config.



5
6
7
8
# File 'lib/sergio/sergio_config.rb', line 5

def initialize
  @parsing_elements = {}
  @new_path, @current_path = [], []
end

Instance Attribute Details

#current_pathObject

Returns the value of attribute current_path.



4
5
6
# File 'lib/sergio/sergio_config.rb', line 4

def current_path
  @current_path
end

#new_pathObject

Returns the value of attribute new_path.



4
5
6
# File 'lib/sergio/sergio_config.rb', line 4

def new_path
  @new_path
end

#sergio_elementsObject

Returns the value of attribute sergio_elements.



4
5
6
# File 'lib/sergio/sergio_config.rb', line 4

def sergio_elements
  @sergio_elements
end

Instance Method Details

#element(name, newname = nil, args = {}, &blk) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sergio/sergio_config.rb', line 10

def element(name, newname = nil, args = {}, &blk)
  if newname.is_a?(Hash)
    args = newname
    newname = nil
  end

  args = args.inject({}) do |args, k_v|
    args[k_v[0].to_sym] = k_v[1]
    args
  end

  args[:attribute] ||= '@text'
  args[:having] ||= false
  newname = name unless newname
  name = [name] unless name.is_a?(Array)
  newname = [newname] unless newname.is_a?(Array)
  aggregate_element = false

  name.each do |n|
    @current_path << n
  end

  newname.each do |n|
    @new_path << n
  end

  unless block_given?
    blk = lambda do |val|
      val
    end
  end

  #block with more calls to #element
  if blk.arity < 1
    blk.call
    callback = lambda {|v|{}}
    aggregate_element = true
  else
    callback = blk
  end

  elem = Sergio::Element.new(new_path, args, callback, aggregate_element)

  @parsing_elements = hash_recursive_merge_to_arrays(@parsing_elements, hash_from_path(current_path, {:sergio_elem => elem}))
  current_path.pop(name.length)
  new_path.pop(newname.length)
end

#get_element_configs(path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sergio/sergio_config.rb', line 58

def get_element_configs(path)
  path = path.clone
  current_elem = path.last
  v = value_at_path(path, @parsing_elements)
  if v
    v = v[:sergio_elem]
    if v
      v = [v] if v.is_a?(Sergio::Element)
      vs = v.select do |v|
        if v.options[:having]
          match = v.options[:having].any? do |attr,value|
            current_elem_attrs = current_elem[1]
            elem_val = current_elem_attrs.assoc(attr.to_s)
            elem_val[1] == value.to_s if elem_val
          end
          true if match
        else
          true
        end
      end
      vs
    end
  end
end