Class: Code::Object::List

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #[], #[]=, #falsy?, #hash, #key?, #truthy?

Constructor Details

#initialize(raw = []) ⇒ List



6
7
8
# File 'lib/code/object/list.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/list.rb', line 4

def raw
  @raw
end

Instance Method Details

#<<(element) ⇒ Object



49
50
51
# File 'lib/code/object/list.rb', line 49

def <<(element)
  raw << element
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
38
39
40
41
42
43
44
45
46
47
# File 'lib/code/object/list.rb', line 10

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

  if operator == "any?"
    any?(arguments, context: context, io: io)
  elsif operator == "none?"
    none?(arguments, context: context, io: io)
  elsif operator == "detect"
    detect(arguments, context: context, io: io)
  elsif operator == "reduce"
    reduce(arguments, context: context, io: io)
  elsif operator == "each"
    each(arguments, context: context, io: io)
  elsif operator == "select"
    select(arguments, context: context, io: io)
  elsif operator == "map"
    map(arguments, context: context, io: io)
  elsif operator == "max"
    max(arguments)
  elsif operator == "flatten"
    flatten(arguments)
  elsif operator == "reverse"
    reverse(arguments)
  elsif operator == "first"
    first(arguments)
  elsif operator == "last"
    last(arguments)
  elsif operator == "max_by"
    max_by(arguments, context: context, io: io)
  elsif operator == "<<"
    append(arguments)
  else
    super
  end
end

#flatten(arguments) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/code/object/list.rb', line 53

def flatten(arguments)
  sig(arguments)
  ::Code::Object::List.new(
    raw.reduce([]) do |acc, element|
      if element.is_a?(::Code::Object::List)
        acc + element.flatten(arguments).raw
      else
        acc + [element]
      end
    end,
  )
end

#inspectObject



70
71
72
# File 'lib/code/object/list.rb', line 70

def inspect
  to_s
end

#to_sObject



66
67
68
# File 'lib/code/object/list.rb', line 66

def to_s
  "[#{raw.map(&:inspect).join(", ")}]"
end