Module: ClassState::Owner

Defined in:
lib/class_state/owner.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/class_state/owner.rb', line 16

def method_missing(method_name, *args)
    # byebug

    if method_name =~ /=$/
        # byebug
        if writer = self.class.state_writers.find{|state_writer| "#{state_writer[:name]}=" == method_name.to_s}
            return self.state[writer[:attribute] || writer[:name]] = args.first
        end
        
        # throw NoMethodError
        return super(method_name, *args)
    end

    if reader = self.class.state_readers.find{|state_reader| state_reader[:name].to_s == method_name.to_s}
        return self.state[reader[:attribute] || reader[:name]] || reader[:default]
    end
    
    # throw NoMethodError
    return super(method_name, *args)
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



6
7
8
# File 'lib/class_state/owner.rb', line 6

def state
  @state
end

Class Method Details

.included(cls) ⇒ Object



8
9
10
# File 'lib/class_state/owner.rb', line 8

def self.included(cls)
    cls.extend(ClassMethods)
end

Instance Method Details

#initialize(_state_values = {}) ⇒ Object



12
13
14
# File 'lib/class_state/owner.rb', line 12

def initialize(_state_values = {})
    @state ||= ClassState.new(_state_values)
end