Class: FinerStruct::Mutable

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Mutable

Returns a new instance of Mutable.



7
8
9
# File 'lib/finer_struct/mutable.rb', line 7

def initialize(attributes)
  @attributes = attributes.dup
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/finer_struct/mutable.rb', line 11

def method_missing(method, *arguments)
  if @attributes.has_key?(method)
    @attributes[method]
  elsif is_assigment?(method) && @attributes.has_key?(key_for_assignment(method))
    @attributes[key_for_assignment(method)] = arguments[0]
  else
    super
  end
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method)
  @attributes.has_key?(method) || super
end

#to_hashObject



25
26
27
# File 'lib/finer_struct/mutable.rb', line 25

def to_hash
  @attributes.dup
end