Skip to content

Commit

Permalink
Clean code
Browse files Browse the repository at this point in the history
  • Loading branch information
atimin committed Nov 5, 2011
1 parent c9afd3a commit 1a3ded9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 32 deletions.
5 changes: 2 additions & 3 deletions lib/lipa/bunch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Lipa
# Implementation of group description
#
# @example
# tree = Lipa::Tree.new :tree do
# tree = root :tree do
# bunch :param_1 => "some_param" do
# leaf :obj_1
# leaf :obj_2
Expand All @@ -45,8 +45,7 @@ def initialize(parent, attrs = {}, &block)
end

def method_missing(name, *args, &block)
args[1] ||= {}
args[1] = @attrs.merge(args[1])
args[1] = @attrs.merge(args[1] || {})
unless Node.add_node(name, @parent, *args, &block)
super
end
Expand Down
11 changes: 3 additions & 8 deletions lib/lipa/kind.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module Lipa
# Implemenation of kind(template) for description
#
# @example
# tree = Lipa::Tree.new :tree do
# tree = root :tree do
# kind :some_kind do
# param1 "some_param"
# end
Expand All @@ -40,13 +40,8 @@ module Lipa
class Kind
attr_reader :attrs, :name, :for, :block
def initialize(name, attrs = {}, &block)
# OPTIMIZE: Make it shorter
if attrs[:for]
@for = attrs[:for]
attrs.delete(:for)
else
@for = :node
end
@for = attrs.delete(:for)
@for ||= :node

@attrs = attrs
@name = name.to_sym
Expand Down
32 changes: 13 additions & 19 deletions lib/lipa/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,13 @@ class Node
@@init_methods = {:node => self}

def initialize(name, attrs = {}, &block)
@attrs = attrs
@name = name.to_s
@children = {}
@parent = attrs[:parent]
@root = attrs[:root]
@full_name = attrs[:full_name]
@kind = attrs[:kind]

#clear attrs
instance_variables.each do |var|
attrs.delete(var[1..-1].to_sym)
end
@parent = attrs.delete(:parent)
@root = attrs.delete(:root)
@full_name = attrs.delete(:full_name)
@kind = attrs.delete(:kind)
@attrs = attrs

instance_eval &block if block_given?
end
Expand Down Expand Up @@ -116,8 +111,8 @@ def eval_attrs
# dir_2["./searched_obj"]
# dir_2["../dir_2/searched_obj"]
def [](path)
split_path = path.split("/")
obj = case split_path[0]
first, *rest_path = path.split("/")
obj = case first
when nil
if path == "/"
root
Expand All @@ -131,15 +126,14 @@ def [](path)
when "."
self
else
children[split_path[0].to_sym]
children[first.to_sym]
end

if obj
if split_path.size > 1
obj[split_path[1..-1].join("/")]
else
obj
end
return nil if obj.nil?
if rest_path.size > 0
obj[rest_path.join("/")]
else
obj
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/lipa/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def self.[](uri)
# @param path to file
#
# @example
# Lipa::Tree.new "lipa" do
# root "lipa" do
# load_from File.dirname(__FILE__) + "/data/part_of_tree.rb"
# end
def load_from(path)
Expand Down
2 changes: 1 addition & 1 deletion spec/kind_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
@tree.folder_1.name.should eql("folder_1")
end

it 'should do_somethign' do
it 'should have attrs' do
@tree.folder_1.some_file.attrs.should eql({:ext => "txt", :size => 1024})
end

Expand Down

0 comments on commit 1a3ded9

Please sign in to comment.