From f200005e920c2e81f496963347e47484fcea180a Mon Sep 17 00:00:00 2001 From: "Glenn Y. Rolland" Date: Sat, 28 Dec 2013 15:31:39 +0100 Subject: [PATCH] Add view creation support. --- lib/kook/app.rb | 12 ++++++++++-- lib/kook/cli.rb | 16 ---------------- lib/kook/project.rb | 26 +++++++++++++++++--------- lib/kook/view.rb | 18 +++++++++--------- test.sh | 2 +- 5 files changed, 37 insertions(+), 37 deletions(-) diff --git a/lib/kook/app.rb b/lib/kook/app.rb index 6cf815a..eb52fc5 100644 --- a/lib/kook/app.rb +++ b/lib/kook/app.rb @@ -63,8 +63,16 @@ module Kook raise MissingProject if not @projects.has_key? project_name - view = View.new view_name - @projects[project_name].add_view view + project_path = @projects[project_name].path + + # simplify if current dir is a subdir of project base + if view_path == project_path then + view_path = '.' + else + view_path.gsub!(/^#{project_path}\//,'') + end + + @projects[project_name].create_view view_name, view_path save end diff --git a/lib/kook/cli.rb b/lib/kook/cli.rb index 76bc21f..033c6ef 100644 --- a/lib/kook/cli.rb +++ b/lib/kook/cli.rb @@ -125,22 +125,6 @@ module Kook @app.add_view project_name, view_name, view_path - project_rootdir = config['projects'][project] - # simplify if current dir is a subdir of project base - if path == project_rootdir then - path = '.' - else - path.gsub!(/^#{project_rootdir}\//,'') - end - - if not config['views'].has_key? project then - config['views'][project] = {} - elsif config['views'][project].nil? then - config['views'][project] = {} - end - #binding.pry - config['views'][project][view] = path - config.save_main end desc "rm PROJECT VIEW", "Unregister existing view on project" diff --git a/lib/kook/project.rb b/lib/kook/project.rb index bf43eb6..7e2c7a5 100644 --- a/lib/kook/project.rb +++ b/lib/kook/project.rb @@ -31,9 +31,17 @@ module Kook @path = path end - def add_view view - raise "ExistingView #{view.name}" if @views.has_key? view.name - @views[view.name] = view + def create_view view_name, view_path + raise ExistingView, view_name if @views.has_key? view_name + View.validate_name view_name + + @views[view_name] = View.new view_name, view_path + end + + def add_view view_data + raise ExistingView, view_data.name if @views.has_key? view_data.name + + @views[view_data.name] = view_data end def remove_view view_name @@ -42,6 +50,7 @@ module Kook end def each_view + pp @views @views.each do |view_name, view_data| yield view_name, view_data end @@ -61,12 +70,11 @@ module Kook p.description = project_hash['description'] p.path = project_path - #project_hash[:views].each do |hash_view| - # view = View.new do |v| - # v.from_hash hash_view - # end - # p.add_view view - #end + #pp project_hash['views'] + project_hash['views'].each do |view_hash| + view_data = View.from_hash view_hash + p.add_view view_data + end end end diff --git a/lib/kook/view.rb b/lib/kook/view.rb index aae7c54..9ab3514 100644 --- a/lib/kook/view.rb +++ b/lib/kook/view.rb @@ -6,10 +6,10 @@ module Kook VIEW_NAME_MIN_SIZE = 4 VIEW_NAME_MAX_SIZE = 12 - def initialize name + def initialize name, path=nil self.class.validate_name name @name = name - @path = nil + @path = path @commands = {} end @@ -24,16 +24,16 @@ module Kook def to_hash return { - view: @name, - path: @path, - commands: @commands.values + 'view' => @name, + 'path' => @path, + 'commands' => @commands.values } end - def from_hash view_hash - @name = view_hash[:view] - @path = view_hash[:path] - @commands = view_hash[:commands] + def self.from_hash view_hash + view = View.new view_hash['view'], view_hash['path'] + # @commands = view_hash['commands'] + view end end end diff --git a/test.sh b/test.sh index 1de773a..df9fa8d 100755 --- a/test.sh +++ b/test.sh @@ -67,4 +67,4 @@ kook project add kook-project $KOOK_OPTS || fail kook view add root $KOOK_OPTS --project kook-project || fail kook view list $KOOK_OPTS --project kook-project || fail -test_cleanup +#test_cleanup