Class: Nyaplot::Layers::LayerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/nyaplot/layers/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = {}) ⇒ LayerBase

Returns a new instance of LayerBase.



21
22
23
24
# File 'lib/nyaplot/layers/base.rb', line 21

def initialize(props={})
  @uuid = SecureRandom.uuid
  @props = props
end

Instance Attribute Details

#uuidObject (readonly)

Returns the value of attribute uuid.



19
20
21
# File 'lib/nyaplot/layers/base.rb', line 19

def uuid
  @uuid
end

Class Method Details

.define_args(*args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nyaplot/layers/base.rb', line 5

def define_args(*args)
  args.each do |s|
    define_method(s) do |val=nil|
      unless val.nil?
        @props[s] = val
        self
      else
        @props[s]
      end
    end
  end
end

Instance Method Details

#to_json(*args) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nyaplot/layers/base.rb', line 33

def to_json(*args)
  is_sync = Proc.new do |l|
    l.is_a?(LayerBase) || ((l.is_a?(Array) && l.all? {|cl| cl.is_a?(LayerBase)}))
  end
  
  args = @props.reduce({}) do |memo, pair|
    memo[pair[0]] = pair[1] unless is_sync.call(pair[1])
    memo
  end
  
  sync_args = @props.reduce({}) do |memo, pair|
    if is_sync.call(pair[1])
      memo[pair[0]] = (pair[1].is_a? LayerBase) ? pair[1].uuid : pair[1].map{|l| l.uuid}
    end
    memo
  end

  {
    type: self.class.name.downcase.split("::").last,
    uuid: @uuid,
    args: args,
    sync_args: sync_args
  }.to_json
end

#to_node(children = []) ⇒ Object



26
27
28
29
30
31
# File 'lib/nyaplot/layers/base.rb', line 26

def to_node(children=[])
  {
    uuid: @uuid,
    children: children
  }
end