Class: FinerStruct::Immutable

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

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Immutable

Returns a new instance of Immutable.



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

def initialize(attributes = {})
  @attributes = attributes.dup.freeze
  freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments) ⇒ Object



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

def method_missing(method, *arguments)
  if has_attribute?(method)
    @attributes[method]
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object



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

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

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

Returns:

  • (Boolean)


17
18
19
# File 'lib/finer_struct/anonymous_immutable.rb', line 17

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

#to_hashObject



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

def to_hash
  @attributes
end