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

Returns a new instance of Base.



9
10
11
12
13
14
15
# File 'lib/compositor/base.rb', line 9

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.



6
7
8
# File 'lib/compositor/base.rb', line 6

def attrs
  @attrs
end

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/compositor/base.rb', line 7

def context
  @context
end

#rootObject

Returns the value of attribute root.



7
8
9
# File 'lib/compositor/base.rb', line 7

def root
  @root
end

Class Method Details

.inherited(subclass) ⇒ Object



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

def self.inherited(subclass)
  method_name = subclass.original_dsl_name
  unless method_name.eql?("base") || method_name.start_with?("abstract")
    # check if it's already defined
    if Compositor::DSL.instance_methods.include?(method_name.to_sym)
      raise MethodAlreadyDefinedError.new("Method #{method_name} is already defined on the DSL class.")
    end
    Compositor::DSL.send(:define_method, method_name) do |*args, &block|
      subclass.
        new(@context, *args).
        dsl(self, &block)
    end
  end
end

.original_dsl_nameObject



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

def self.original_dsl_name
  self.name.gsub(/(Compositor$)/, '').gsub(/::/, '_').underscore
end

Instance Method Details

#dslObject



56
57
58
# File 'lib/compositor/base.rb', line 56

def dsl
  raise "Implement in subclasses"
end

#include_root?Boolean

Returns:

  • (Boolean)


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

def include_root?
  self.root ? true : false
end

#to_hObject



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

def to_h
  to_hash
end

#to_hashObject

Raises:

  • (NotImplementedError)


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

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

#to_json(options = {}) ⇒ Object



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

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

#with_root_elementObject



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

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