diff --git a/.rubocop.yml b/.rubocop.yml index 3d98fc8..0bb63a8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -41,3 +41,6 @@ Documentation: Rails: Enabled: true + +Metrics/ClassLength: + Max: 150 diff --git a/lib/easydsl/node.rb b/lib/easydsl/node.rb index eedb9d9..9caf895 100644 --- a/lib/easydsl/node.rb +++ b/lib/easydsl/node.rb @@ -3,11 +3,12 @@ module Easydsl class Node - attr_reader :name, :index, :parent + attr_reader :name, :index, :parent, :singleton attr_accessor :args def initialize(name, args, index, parent, node_builders = []) - @name = name + @name = name.to_s.chomp('!').to_sym + @singleton = name[-1] == '!' @args = args @index = index @parent = parent @@ -30,23 +31,41 @@ def max_index all_nodes.map(&:index).max || 0 end + def define(&block) + add_block(&block) + end + + protected + + def add_hierarchy(to, tree, base_index) + tree.each_with_index do |item, index| + to.add_child(item.name, item.args, base_index + index, to, item.nodes) + end + end + def add_block(&block) tree = NodeBuilder.new('tree') tree.instance_exec(&block) - base_index = max_index - tree.nodes.each_with_index do |item, index| - add_child(item.name, item.args, base_index + index, item.nodes) - end + add_hierarchy(self, tree.nodes, max_index) end - protected - def add_child(name, args, index, parent, node_builders = []) + node = handle_singleton(name, args, node_builders) + return node unless node.nil? node = Node.new(name, args, index, parent, node_builders) nodes[node.name] << node node end + def handle_singleton(name, args, node_builders = []) + return nil unless nodes.key?(name) + node = nodes[name].first + return nil if node.nil? || node.singleton == false + node.args = args + add_hierarchy(node, node_builders, node.max_index) + nodes[name].first + end + def clean_method_symbol(method_symbol) method_symbol.to_s.gsub(/[=?]/, '').to_sym end diff --git a/lib/easydsl/version.rb b/lib/easydsl/version.rb index 62c6917..f93b09d 100644 --- a/lib/easydsl/version.rb +++ b/lib/easydsl/version.rb @@ -1,3 +1,3 @@ module Easydsl - VERSION = '0.1.0'.freeze + VERSION = '0.1.1'.freeze end diff --git a/spec/easydsl_spec.rb b/spec/easydsl_spec.rb index b131382..cd7b766 100644 --- a/spec/easydsl_spec.rb +++ b/spec/easydsl_spec.rb @@ -86,6 +86,12 @@ class Post it 'returns the first element if multiple present' do expect(dsl.navbar).not_to be_nil end + + it 'returns the value if called directly' do + node = dsl.config.title + expect(node).to eq('test title') + expect(dsl.config.title).to eq('test title') + end end context 'Members override' do diff --git a/spec/operators_spec.rb b/spec/operators_spec.rb index 547aef2..209730b 100644 --- a/spec/operators_spec.rb +++ b/spec/operators_spec.rb @@ -57,7 +57,7 @@ it 'set a block to root' do expect do - dsl.add_block do + dsl.define do item 'item 3' end end.to change { @@ -118,6 +118,51 @@ end end + context 'Operator !' do + let(:dsl) do + Easydsl.define do + config! :hello do + title! 'test' + environment :debug + end + item do + layout :left + end + item + end + end + + it 'set singleton flags correctly' do + expect(dsl.config.singleton).to be(true) + expect(dsl.item.singleton).to be(false) + end + + it 'modify the existing root node' do + dsl.define do + config do + title 'changed' + new_property 'hello' + end + end + expect(dsl.configs.count).to eq(1) + expect(dsl.config.nodes.count).to eq(3) + expect(dsl.config.title).to eq('changed') + expect(dsl.config.environment).to eq(:debug) + expect(dsl.config.new_property).to eq('hello') + end + + it 'modify the existing nested node' do + expect(dsl.config.nodes.count).to eq(2) + expect(dsl.config.title).to eq('test') + dsl.config do + title 'changed' + end + expect(dsl.config.nodes.count).to eq(2) + expect(dsl.config.title).to eq('changed') + expect(dsl.config.environment).to eq(:debug) + end + end + context 'Operator ?' do let(:dsl) do Easydsl.define do