Class: Mothership::Inputs
- Inherits:
-
Object
- Object
- Mothership::Inputs
- Defined in:
- lib/mothership/inputs.rb
Instance Attribute Summary collapse
-
#inputs ⇒ Object
readonly
Returns the value of attribute inputs.
Instance Method Summary collapse
- #[](name, *args) ⇒ Object
- #forget(name) ⇒ Object
- #get(name, context, *args) ⇒ Object
- #given(name) ⇒ Object
- #given?(name) ⇒ Boolean
-
#initialize(command, context = nil, inputs = {}) ⇒ Inputs
constructor
A new instance of Inputs.
- #merge(inputs) ⇒ Object
- #without(*names) ⇒ Object
Constructor Details
#initialize(command, context = nil, inputs = {}) ⇒ Inputs
Returns a new instance of Inputs.
5 6 7 8 9 10 |
# File 'lib/mothership/inputs.rb', line 5 def initialize(command, context = nil, inputs = {}) @command = command @context = context @inputs = inputs @cache = {} end |
Instance Attribute Details
#inputs ⇒ Object (readonly)
Returns the value of attribute inputs.
3 4 5 |
# File 'lib/mothership/inputs.rb', line 3 def inputs @inputs end |
Instance Method Details
#[](name, *args) ⇒ Object
33 34 35 |
# File 'lib/mothership/inputs.rb', line 33 def [](name, *args) get(name, @context, *args) end |
#forget(name) ⇒ Object
86 87 88 89 |
# File 'lib/mothership/inputs.rb', line 86 def forget(name) @cache.delete(name) @inputs.delete(name) end |
#get(name, context, *args) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/mothership/inputs.rb', line 37 def get(name, context, *args) return @cache[name] if @cache.key? name = @command.inputs[name] return unless if @inputs.key?(name) && @inputs[name] != [] val = if convert = [:from_given] if @inputs[name].is_a?(Array) @inputs[name].collect do |i| @context.instance_exec(i, *args, &convert) end else @context.instance_exec(@inputs[name], *args, &convert) end else @inputs[name] end return @cache[name] = val end val = if [:default].respond_to? :to_proc unless context raise "no context for input request" end context.instance_exec(*args, &[:default]) elsif [:default] [:default] elsif [:type] == :boolean false elsif [:argument] == :splat if [:singular] && single = @inputs[[:singular]] [single] else [] end end unless [:forget] @cache[name] = val end val end |
#given(name) ⇒ Object
16 17 18 |
# File 'lib/mothership/inputs.rb', line 16 def given(name) @inputs[name] end |
#given?(name) ⇒ Boolean
12 13 14 |
# File 'lib/mothership/inputs.rb', line 12 def given?(name) @inputs.key?(name) end |
#merge(inputs) ⇒ Object
20 21 22 |
# File 'lib/mothership/inputs.rb', line 20 def merge(inputs) self.class.new(@command, @context, @inputs.merge(inputs)) end |
#without(*names) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/mothership/inputs.rb', line 24 def without(*names) inputs = @inputs.dup names.each do |n| inputs.delete(n) end self.class.new(@command, @context, inputs) end |