Module: Agrippa::Immutable::InstanceMethods

Defined in:
lib/agrippa/immutable.rb

Instance Method Summary collapse

Instance Method Details

#__symbolize_keys(input) ⇒ Object



72
73
74
75
76
77
# File 'lib/agrippa/immutable.rb', line 72

def __symbolize_keys(input)
    output = input.dup
    input.keys { |k| output[k.to_sym] = output.delete(k) \
        unless k.is_a?(Symbol) }
    output
end

#chain(updates) ⇒ Object

Raises:

  • (ArgementError)


58
59
60
61
62
# File 'lib/agrippa/immutable.rb', line 58

def chain(updates)
    raise(ArgementError, "#set requires a Hash") \
        unless updates.respond_to?(:each_pair)
    self.class.new(@state.merge(__symbolize_keys(updates)), false)
end

#fetch(key, default = nil) ⇒ Object



68
69
70
# File 'lib/agrippa/immutable.rb', line 68

def fetch(key, default = nil)
    @state.fetch(key.to_sym, default)
end

#initialize(state = Hamster::Hash.new, apply_default = true) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/agrippa/immutable.rb', line 45

def initialize(state = Hamster::Hash.new, apply_default = true)
    raise(ArgumentError, "#{self.class}#new requires a hash.") \
        unless state.respond_to?(:each_pair)
    if(apply_default and respond_to?(:default_state))
        @state = Hamster::Hash.new(default_state)
        @state = @state.merge(state) unless state.nil?
    elsif(state.is_a?(Hamster::Hash))
        @state = state
    else
        @state = Hamster::Hash.new(__symbolize_keys(state))
    end
end

#store(key, value) ⇒ Object



64
65
66
# File 'lib/agrippa/immutable.rb', line 64

def store(key, value)
    self.class.new(@state.store(key.to_sym, value), false)
end