Class: Tfrb::Block

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBlock

Returns a new instance of Block.



7
8
9
# File 'lib/tfrb/block.rb', line 7

def initialize
  @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/tfrb/block.rb', line 19

def method_missing(method_name, *args, &block)
  method_name = method_name.to_s.gsub(/_$/, '').to_sym if method_name.to_s =~ /_$/
  if block_given?
    @data[method_name.to_s] ||= {}
    instance = self.class.new
    instance.instance_eval(&block)
    if args && args.size > 0
      hash = args[0..-2].reduce(@data[method_name.to_s]) { |h, v| h[v] ||= {} }
      hash[args.last] = instance.to_h
    else
      @data[method_name.to_s] = instance.to_h
    end
    # puts "method missing called for #{method_name}[#{args}] with block: #{instance.to_h}"
  else
    @data.merge!({ method_name.to_s => args[0] })
    # puts "method missing called for #{method_name}[#{args}] without block"
  end
end

Class Method Details

.load(filename) ⇒ Object



38
39
40
41
42
# File 'lib/tfrb/block.rb', line 38

def self.load(filename)
  instance = self.new
  instance.import(filename)
  instance.to_h
end

Instance Method Details

#import(filename, params = {}) ⇒ Object



15
16
17
# File 'lib/tfrb/block.rb', line 15

def import(filename, params = {})
  instance_eval(IO.read(File.join(Tfrb::Config[:path], "#{filename}.rb")), filename)
end

#to_hObject



11
12
13
# File 'lib/tfrb/block.rb', line 11

def to_h
  @data
end