Class: FinerStruct::Mutable

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Mutable

Returns a new instance of Mutable.



4
5
6
# File 'lib/finer_struct/anonymous_mutable.rb', line 4

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



8
9
10
11
12
13
14
15
16
# File 'lib/finer_struct/anonymous_mutable.rb', line 8

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

Instance Method Details

#==(other) ⇒ Object



26
27
28
# File 'lib/finer_struct/anonymous_mutable.rb', line 26

def ==(other)
  other.class == self.class && other.to_hash == to_hash
end

#respond_to?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/finer_struct/anonymous_mutable.rb', line 18

def respond_to?(method, include_all = false)
  has_attribute?(method) || super
end

#to_hashObject



22
23
24
# File 'lib/finer_struct/anonymous_mutable.rb', line 22

def to_hash
  @attributes.dup
end