Class: Eco::Language::Models::Wrap

Inherits:
Object
  • Object
show all
Defined in:
lib/eco/language/models/wrap.rb

Defined Under Namespace

Modules: Test

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object = nil, setters: [], getters: []) ⇒ Wrap

Returns a new instance of Wrap.



9
10
11
12
13
# File 'lib/eco/language/models/wrap.rb', line 9

def initialize(object = nil, setters: [], getters: [])
  @object = object
  self.setters = setters
  self.getters = getters
end

Instance Attribute Details

#gettersObject

Returns the value of attribute getters.



6
7
8
# File 'lib/eco/language/models/wrap.rb', line 6

def getters
  @getters
end

#objectObject

Returns the value of attribute object.



7
8
9
# File 'lib/eco/language/models/wrap.rb', line 7

def object
  @object
end

#settersObject

Returns the value of attribute setters.



6
7
8
# File 'lib/eco/language/models/wrap.rb', line 6

def setters
  @setters
end

Class Method Details

.testObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/eco/language/models/wrap.rb', line 74

def self.test()
  # setters
  soa = ->(o,v) { o.a = v }
  soe = ->(o,v) { o.e = v }
  # getters
  goa  = ->(o) { o.a }
  goe  = ->(o) { o.e }
  goex = ->(o) { o.exp }

  # arrays
  setters = [soa, soe ]
  getters = [goa, goe, goex]

  a = Test::A.new(3)
  b = Test::A.new(5)
  w = self.new(a, setters: setters, getters: getters)

  puts "before check  -> a: #{a.a}; e: #{a.e}; exp: #{a.exp}"
  # launch setters
  w << [4, 2]
  puts "after setters -> a: #{a.a}; e: #{a.e}; exp: #{a.exp}"
  # launch getters
  a, e, exp = w.row
  puts "from getters(a) -> a: #{a}; e: #{e}; exp: #{exp}"
  # switch object
  w.object = b
  # launch getters
  a, e, exp = w.row
  puts "from getters(b) -> a: #{a}; e: #{e}; exp: #{exp}"
end

Instance Method Details

#<<(value = []) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/eco/language/models/wrap.rb', line 31

def <<(value = [])
  value = into_a(value)
  @wsetters.each_with_index.map do |set, i|
    args = into_a(value[i])
    set.call(*args)
  end
end

#row(value = (miss = true; [])) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/eco/language/models/wrap.rb', line 39

def row(value = (miss = true; []))
  value = into_a(value)
  @wgetters.each_with_index.map do |get, i|
    args = into_a(value[i])
    get.call(*args) unless miss
    get.call
  end
end

#wrap_object(object = nil, ops = []) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/eco/language/models/wrap.rb', line 48

def wrap_object(object = nil, ops = [])
  object = object || @object
  return [] unless !!object
  into_a(ops).map do |op|
    cop = required_parameters?(op) ? curry(op) : op
    ->(*args) {
      args.unshift(object);
      #puts "calling wop: #{cop}, with args: #{args}, object: #{object}"
      cop[*args] #rescue nil
    }
  end
end