Class: Compositor::Base

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

Direct Known Subclasses

Composite, Leaf

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context, attrs = {}) ⇒ Base



6
7
8
9
10
11
12
# File 'lib/compositor/base.rb', line 6

def initialize(view_context, attrs = {})
  @attrs = attrs
  self.context = view_context
  attrs.each_pair do |key, value|
    self.send("#{key}=", value)
  end
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



3
4
5
# File 'lib/compositor/base.rb', line 3

def attrs
  @attrs
end

#contextObject

Returns the value of attribute context.



4
5
6
# File 'lib/compositor/base.rb', line 4

def context
  @context
end

#rootObject

Returns the value of attribute root.



4
5
6
# File 'lib/compositor/base.rb', line 4

def root
  @root
end

Class Method Details

.inherited(subclass) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/compositor/base.rb', line 42

def self.inherited(subclass)
  method_name = root_class_name(subclass)
  unless method_name.eql?("base") # check if it's already defined
    Compositor::DSL.send(:define_method, method_name) do |*args, &block|
      subclass.
        new(@context, *args).
        dsl(self, &block)
    end
  end
end

.root_class_name(klazz) ⇒ Object



38
39
40
# File 'lib/compositor/base.rb', line 38

def self.root_class_name(klazz)
  klazz.name.gsub(/(.*::)|(Compositor$)/, '').underscore
end

Instance Method Details

#dslObject



53
54
55
# File 'lib/compositor/base.rb', line 53

def dsl
  raise "Implement in subclasses"
end

#include_root?Boolean



30
31
32
# File 'lib/compositor/base.rb', line 30

def include_root?
  self.root ? true : false
end

#root_class_nameObject



34
35
36
# File 'lib/compositor/base.rb', line 34

def root_class_name
  self.class.root_class_name(self.class)
end

#to_hObject



22
23
24
# File 'lib/compositor/base.rb', line 22

def to_h
  to_hash
end

#to_hashObject

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/compositor/base.rb', line 14

def to_hash
  raise NotImplementedError.new("Abstract method, should be implemented by subclasses")
end

#to_json(options = {}) ⇒ Object



26
27
28
# File 'lib/compositor/base.rb', line 26

def to_json(options = {})
  Oj.dump(to_hash)
end

#with_root_elementObject



18
19
20
# File 'lib/compositor/base.rb', line 18

def with_root_element
  include_root? ? {root => yield} : yield
end