Class: Code::Object::Dictionnary

Inherits:
Code::Object show all
Defined in:
lib/code/object/dictionnary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #falsy?, #hash, #truthy?

Constructor Details

#initialize(raw = {}) ⇒ Dictionnary

Returns a new instance of Dictionnary.



6
7
8
# File 'lib/code/object/dictionnary.rb', line 6

def initialize(raw = {})
  @raw = raw
end

Instance Attribute Details

#rawObject (readonly)

Returns the value of attribute raw.



4
5
6
# File 'lib/code/object/dictionnary.rb', line 4

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object



27
28
29
# File 'lib/code/object/dictionnary.rb', line 27

def [](key)
  raw[key]
end

#[]=(key, value) ⇒ Object



31
32
33
# File 'lib/code/object/dictionnary.rb', line 31

def []=(key, value)
  raw[key] = value
end

#call(**args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/code/object/dictionnary.rb', line 10

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])

  if operator == "values"
    values(arguments)
  elsif key?(operator)
    fetch(operator)
  else
    super
  end
end

#fetch(key) ⇒ Object



23
24
25
# File 'lib/code/object/dictionnary.rb', line 23

def fetch(key)
  raw.fetch(key)
end

#inspectObject



43
44
45
# File 'lib/code/object/dictionnary.rb', line 43

def inspect
  to_s
end

#key?(key) ⇒ Boolean

Returns:



35
36
37
# File 'lib/code/object/dictionnary.rb', line 35

def key?(key)
  raw.key?(key)
end

#to_sObject



39
40
41
# File 'lib/code/object/dictionnary.rb', line 39

def to_s
  "{#{raw.map { |key, value| "#{key.inspect} => #{value.inspect}" }.join(", ")}}"
end