Class: Dslh::Scope

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/dslh.rb', line 152

def method_missing(method_name, *args, &block)
  key_conv = @__options__[:key_conv] || @__options__[:conv]
  value_conv = @__options__[:value_conv] || @__options__[:conv]

  nested_hash = nil

  if block
    hash_orig = @__hash__
    @__hash__ = {}
    self.instance_eval(&block)
    nested_hash = @__hash__
    @__hash__ = hash_orig
  end

  method_name = key_conv.call(method_name) if key_conv

  if args.empty?
    @__hash__[method_name] = nested_hash
  else
    args = args.map {|i| value_conv.call(i) } if value_conv
    value = args.length > 1 ? args : args[0]

    if nested_hash
      @__hash__[method_name] = {
      value => nested_hash
    }
    else
      @__hash__[method_name] = value
    end

    return @__hash__
  end
end

Instance Method Details

#_(&block) ⇒ Object



141
142
143
144
145
146
147
148
149
150
# File 'lib/dslh.rb', line 141

def _(&block)
  if block
    hash_orig = @__hash__
    @__hash__ = {}
    self.instance_eval(&block)
    nested_hash = @__hash__
    @__hash__ = hash_orig
    return nested_hash
  end
end