Class: AwesomeXmlDsl::OptionsParser

Inherits:
Object
  • Object
show all
Defined in:
lib/awesome_xml_dsl/options_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(original_options, parent_options = {}) ⇒ OptionsParser

Returns a new instance of OptionsParser.



7
8
9
10
11
12
13
14
# File 'lib/awesome_xml_dsl/options_parser.rb', line 7

def initialize(original_options, parent_options = {})
  self.options = original_options.clone
  options[:locals] ||= {}

  return unless parent_options.key?(:locals)

  options[:locals] = parent_options[:locals].deep_merge(options[:locals])
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/awesome_xml_dsl/options_parser.rb', line 5

def options
  @options
end

Class Method Details

.parse(options, parent_options = {}) ⇒ Object



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

def self.parse(options, parent_options = {})
  new options, parent_options
end

Instance Method Details

#can_read?(object, key) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/awesome_xml_dsl/options_parser.rb', line 61

def can_read?(object, key)
  return object.key?(key) if object.respond_to?(:key?)

  object.respond_to?(key)
end

#delete_options(*keys) ⇒ Object



55
56
57
58
59
# File 'lib/awesome_xml_dsl/options_parser.rb', line 55

def delete_options(*keys)
  keys.each do |key|
    options.delete key
  end
end

#eachObject



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
# File 'lib/awesome_xml_dsl/options_parser.rb', line 20

def each
  return if options.key?(:if) && !options[:if]

  if options.key?(:collection) && options.key?(:as)
    return unless options[:collection].respond_to? :each

    as = options[:as]
    collection = options[:collection]
    delete_options :collection, :as

    collection.each do |element|
      options[:locals][as] = element

      yield options
    end
  elsif (options.key?(:if) || options.key?(:require)) && options.key?(:of)
    return if options.key?(:if) && !(can_read? options[:of], options[:if])

    if options.key?(:require) && !can_read?(options[:of], options[:require])
      raise RequiredValueNotPresent, "Can't read required key #{options[:require]} of #{options[:of]}"
    end

    accessor = options[:require] || options[:if]
    of = options[:of]
    as = options[:as] || accessor

    delete_options :if, :of, :as, :require

    options[:locals][as] = read of, accessor
    yield options
  else
    yield options
  end
end

#read(object, key) ⇒ Object



67
68
69
70
71
# File 'lib/awesome_xml_dsl/options_parser.rb', line 67

def read(object, key)
  return object[key] if object.is_a? Hash

  object.send key
end