Skip to content

Commit

Permalink
Added singleton nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
MirkoMignini committed Mar 31, 2016
1 parent f78abc5 commit 6f54abc
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ Documentation:

Rails:
Enabled: true

Metrics/ClassLength:
Max: 150
35 changes: 27 additions & 8 deletions lib/easydsl/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/easydsl/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Easydsl
VERSION = '0.1.0'.freeze
VERSION = '0.1.1'.freeze
end
6 changes: 6 additions & 0 deletions spec/easydsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 46 additions & 1 deletion spec/operators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6f54abc

Please sign in to comment.