Class: MVCLI::Form::Input::Target
- Inherits:
-
Object
- Object
- MVCLI::Form::Input::Target
- Defined in:
- lib/mvcli/form/input.rb
Direct Known Subclasses
Instance Method Summary collapse
- #decode(&block) ⇒ Object
- #default(context) ⇒ Object
-
#initialize(name, target, options = {}, &block) ⇒ Target
constructor
A new instance of Target.
- #value(source, context = nil) ⇒ Object
Constructor Details
#initialize(name, target, options = {}, &block) ⇒ Target
Returns a new instance of Target.
24 25 26 27 28 29 30 |
# File 'lib/mvcli/form/input.rb', line 24 def initialize(name, target, = {}, &block) @name, @options = name, Map() @decoders = [] if block_given? @decoders << block end end |
Instance Method Details
#decode(&block) ⇒ Object
32 33 34 |
# File 'lib/mvcli/form/input.rb', line 32 def decode(&block) @decoders << block end |
#default(context) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/mvcli/form/input.rb', line 46 def default(context) value = @options[:default] if value.respond_to?(:call) if context context.instance_exec(&value) else value.call end else value end end |
#value(source, context = nil) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/mvcli/form/input.rb', line 36 def value(source, context = nil) if value = [source[@name]].flatten.first @decoders.reduce(value) do |value, decoder| decoder.call value end else default context end end |