Class: Compositor::Base
- Inherits:
-
Object
- Object
- Compositor::Base
- Defined in:
- lib/compositor/base.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#context ⇒ Object
Returns the value of attribute context.
-
#root ⇒ Object
Returns the value of attribute root.
Class Method Summary collapse
Instance Method Summary collapse
- #dsl ⇒ Object
- #include_root? ⇒ Boolean
-
#initialize(view_context, attrs = {}) ⇒ Base
constructor
A new instance of Base.
- #root_class_name ⇒ Object
- #to_h ⇒ Object
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ Object
- #with_root_element ⇒ Object
Constructor Details
#initialize(view_context, attrs = {}) ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 14 |
# File 'lib/compositor/base.rb', line 8 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
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
5 6 7 |
# File 'lib/compositor/base.rb', line 5 def attrs @attrs end |
#context ⇒ Object
Returns the value of attribute context.
6 7 8 |
# File 'lib/compositor/base.rb', line 6 def context @context end |
#root ⇒ Object
Returns the value of attribute root.
6 7 8 |
# File 'lib/compositor/base.rb', line 6 def root @root end |
Class Method Details
.inherited(subclass) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/compositor/base.rb', line 44 def self.inherited(subclass) method_name = root_class_name(subclass) 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 |
.root_class_name(klazz) ⇒ Object
40 41 42 |
# File 'lib/compositor/base.rb', line 40 def self.root_class_name(klazz) klazz.name.gsub(/(.*::)|(Compositor$)/, '').underscore end |
Instance Method Details
#dsl ⇒ Object
59 60 61 |
# File 'lib/compositor/base.rb', line 59 def dsl raise "Implement in subclasses" end |
#include_root? ⇒ Boolean
32 33 34 |
# File 'lib/compositor/base.rb', line 32 def include_root? self.root ? true : false end |
#root_class_name ⇒ Object
36 37 38 |
# File 'lib/compositor/base.rb', line 36 def root_class_name self.class.root_class_name(self.class) end |
#to_h ⇒ Object
24 25 26 |
# File 'lib/compositor/base.rb', line 24 def to_h to_hash end |
#to_hash ⇒ Object
16 17 18 |
# File 'lib/compositor/base.rb', line 16 def to_hash raise NotImplementedError.new("Abstract method, should be implemented by subclasses") end |
#to_json(options = {}) ⇒ Object
28 29 30 |
# File 'lib/compositor/base.rb', line 28 def to_json( = {}) Oj.dump(to_hash) end |
#with_root_element ⇒ Object
20 21 22 |
# File 'lib/compositor/base.rb', line 20 def with_root_element include_root? ? {root => yield} : yield end |