Module: Babby::MetaDSL::ClassMethods

Defined in:
lib/babby.rb

Instance Method Summary collapse

Instance Method Details

#apply_to(entity_method_name) ⇒ Object



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

def apply_to entity_method_name
  # Set the name of the method to retrieve the entity bound to the instance, as a symbol
end

#dsl_method(name, type: nil) ⇒ Object



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
54
55
56
57
58
59
60
# File 'lib/babby.rb', line 25

def dsl_method name, type: nil
  state_type = type ? STATE_TYPE_MAP[type] : nil

  if type == :array
    define_singleton_method name do |value| # define 'setter' for derived class - i.e. DSL 'setup' method a la attr_accessor
      if not self.instance_variable_get "@#{name}"
        self.instance_variable_set "@#{name}", state_type ? state_type.new : nil
      end

      self.instance_variable_get("@#{name}").push(value)  # Set the underlying class instance variable to the desired value
    end
  else
    define_singleton_method name do |value| # define 'setter' for derived class - i.e. DSL 'setup' method a la attr_accessor
      if not self.instance_variable_get  "@#{name}"
        self.instance_variable_set "@#{name}", state_type ? state_type.new : nil
      end

      self.instance_variable_set "@#{name}", value  # Set the underlying class instance variable to the desired value
    end
  end

  define_singleton_method "get_#{name}".to_sym do
    self.instance_variable_get "@#{name}"
  end

  if type == :proc
    define_method name do
      proc = self.class.send "get_#{name}".to_sym
      proc.call(entity)
    end
  else
    define_method name do
      self.class.send "get_#{name}".to_sym
    end
  end
end