pakyow-presenter/lib/presenter/binder_set.rb in pakyow-presenter-0.8.0 vs pakyow-presenter/lib/presenter/binder_set.rb in pakyow-presenter-0.9.0

- old
+ new

@@ -1,35 +1,41 @@ module Pakyow module Presenter class BinderSet attr_reader :scopes - def initialize + def initialize(&block) @scopes = {} @options = {} + @config = {} + + instance_exec(&block) end def scope(name, &block) scope_eval = ScopeEval.new - bindings, options = scope_eval.eval(&block) + bindings, options, config = scope_eval.eval(&block) @scopes[name.to_sym] = bindings @options[name.to_sym] = options + @config[name.to_sym] = config end def match_for_prop(prop, scope, bindable, bindings = {}) return bindings_for_scope(scope, bindings)[prop] end - def options_for_prop(prop, scope, bindable, context) + def options_for_prop(scope, prop, bindable, context) if block = (@options[scope] || {})[prop] - binding_eval = BindingEval.new(bindable, prop, context) - binding_eval.instance_exec(&block) + binding_eval = BindingEval.new(prop, bindable, context) + values = binding_eval.instance_exec(binding_eval.value, bindable, context, &block) + values.unshift(['', '']) if @config[scope][prop][:empty] + values end end - def has_prop?(prop, scope, bindings) + def has_prop?(scope, prop, bindings) bindings_for_scope(scope, bindings).key? prop end def bindings_for_scope(scope, bindings) # merge passed bindings with bindings @@ -41,33 +47,35 @@ include Helpers def initialize @bindings = {} @options = {} + @config = {} end def eval(&block) self.instance_eval(&block) - return @bindings, @options + return @bindings, @options, @config end def binding(name, &block) @bindings[name.to_sym] = block end - def options(name, &block) + def options(name, empty: false, &block) @options[name] = block + @config[name] = { empty: empty } end def restful(route_group) binding(:action) { - routes = router.group(route_group) + routes = Router.instance.group(route_group) return_data = {} if id = bindable[:id] return_data[:view] = lambda { |view| - view.prepend(View.from_doc(Nokogiri::HTML.fragment('<input type="hidden" name="_method" value="patch">'))) + view.prepend(View.from_doc(NokogiriDoc.from_doc(Nokogiri::HTML.fragment('<input type="hidden" name="_method" value="patch">')))) } action = routes.path(:update, :"#{route_group}_id" => id) else action = routes.path(:create) @@ -78,25 +86,8 @@ return_data } end #TODO options - end - - class BindingEval - include Helpers - - attr_accessor :context - attr_reader :bindable - - def initialize(prop, bindable, context) - @prop = prop - @bindable = bindable - @context = context - end - - def value - bindable[@prop] - end end end end