Class: Blockster::Context

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

Instance Method Summary collapse

Constructor Details

#initialize(temp_class, wrapper) ⇒ Context

Returns a new instance of Context.



5
6
7
8
9
10
11
12
13
14
# File 'lib/blockster/context.rb', line 5

def initialize(temp_class, wrapper)
  @temp_class = temp_class
  @wrapper = wrapper
  @temp_class.class_eval do
    class << self
      attr_accessor :nested_attributes
    end
    @nested_attributes = {}
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



29
30
31
32
33
34
35
# File 'lib/blockster/context.rb', line 29

def method_missing(method_name, *args, &block)
  if @temp_class.respond_to?(method_name, true)
    @temp_class.send(method_name, *args, &block)
  else
    super
  end
end

Instance Method Details

#attribute(name, type, **_options) ⇒ Object



21
22
23
# File 'lib/blockster/context.rb', line 21

def attribute(name, type, **_options)
  @temp_class.attribute(name, type)
end

#nested(name, &block) ⇒ Object



25
26
27
# File 'lib/blockster/context.rb', line 25

def nested(name, &block)
  @temp_class.nested_attributes[name] = block
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/blockster/context.rb', line 37

def respond_to_missing?(method_name, include_private = false)
  @temp_class.respond_to?(method_name, include_private) || super
end

#root(key, &block) ⇒ Object



16
17
18
19
# File 'lib/blockster/context.rb', line 16

def root(key, &block)
  @wrapper.instance_variable_set("@root_key", key)
  instance_eval(&block)
end