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



47
48
49
# File 'lib/code/object/dictionnary.rb', line 47

def [](key)
  raw[key]
end

#[]=(key, value) ⇒ Object



51
52
53
# File 'lib/code/object/dictionnary.rb', line 51

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

#call(**args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/code/object/dictionnary.rb', line 10

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  globals = multi_fetch(args, *::Code::GLOBALS)
  value = arguments.first&.value

  if operator == "values"
    sig(arguments)
    values
  elsif operator == "keys"
    sig(arguments)
    keys
  elsif operator == "each"
    sig(arguments) { ::Code::Object::Function }
    each(value, **globals)
  elsif key?(operator)
    result = fetch(operator)

    if result.is_a?(::Code::Object::Function)
      result.call(**args.merge(operator: nil))
    else
      sig(arguments)
      result
    end
  else
    super
  end
end

#deep_dupObject



59
60
61
# File 'lib/code/object/dictionnary.rb', line 59

def deep_dup
  ::Code::Object::Dictionnary.new(raw.deep_dup)
end

#fetch(key) ⇒ Object



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

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

#inspectObject



67
68
69
# File 'lib/code/object/dictionnary.rb', line 67

def inspect
  to_s
end

#key?(key) ⇒ Boolean

Returns:



55
56
57
# File 'lib/code/object/dictionnary.rb', line 55

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

#merge(other) ⇒ Object



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

def merge(other)
  ::Code::Object::Dictionnary.new(raw.merge(other.raw))
end

#to_sObject



63
64
65
# File 'lib/code/object/dictionnary.rb', line 63

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