lib/compositor/base.rb in compositor-0.1.1 vs lib/compositor/base.rb in compositor-0.1.3

- old
+ new

@@ -1,13 +1,13 @@ module Compositor class Base attr_reader :attrs - attr_accessor :root, :view_context + attr_accessor :root, :context def initialize(view_context, attrs = {}) @attrs = attrs - self.view_context = view_context + self.context = view_context attrs.each_pair do |key, value| self.send("#{key}=", value) end end @@ -21,14 +21,10 @@ def to_h to_hash end - def collection_to_generator(klazz, collection) - collection.map { |o| klazz.new(view_context) } - end - def to_json(options = {}) Oj.dump(to_hash) end def include_root? @@ -38,15 +34,31 @@ def root_class_name self.class.root_class_name(self.class) end def self.root_class_name(klazz) - klazz.name.gsub(/.*::/, '').underscore + klazz.name.gsub(/(.*::)|(Compositor$)/, '').underscore end + + 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 + + def dsl + raise "Implement in subclasses" + end end end -require_relative 'leaf' -require_relative 'composite' require_relative 'dsl' +require_relative 'composite' +require_relative 'leaf' +require_relative 'literal' require_relative 'list' -require_relative 'hash' +require_relative 'map'