Class: Orchparty::HashBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/orchparty/dsl_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeHashBuilder

Returns a new instance of HashBuilder.



119
120
# File 'lib/orchparty/dsl_parser.rb', line 119

def initialize
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(_, *values, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/orchparty/dsl_parser.rb', line 122

def method_missing(_, *values, &block)
  if block_given?
    builder = HashBuilder.new
    builder.instance_eval(&block)
    value = builder._build
    if values.count == 1
      @hash ||= {}
      @hash[values.first.to_sym] = value
    else
      @hash ||= []
      @hash << value
    end
  else
    value = values.first
    if value.is_a? Hash
      @hash ||= {}
      key, value = value.first
      @hash[key.to_sym] = value
    else
      @hash ||= []
      @hash << value
    end
  end
  self
end

Instance Method Details

#_buildObject



148
149
150
# File 'lib/orchparty/dsl_parser.rb', line 148

def _build
  @hash || {}
end