Class: MidasTouch::InputGroup
- Inherits:
-
Object
- Object
- MidasTouch::InputGroup
- Includes:
- Enumerable
- Defined in:
- lib/midas-touch/input_group.rb
Instance Attribute Summary collapse
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#inputs ⇒ Object
Returns the value of attribute inputs.
-
#validations ⇒ Object
Returns the value of attribute validations.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #each(&block) ⇒ Object
- #error_on?(name) ⇒ Boolean
- #fields ⇒ Object (also: #keys)
- #filter! ⇒ Object
-
#initialize ⇒ InputGroup
constructor
A new instance of InputGroup.
- #input(name) ⇒ Object
- #invalidate_input(input_name) ⇒ Object
- #to_h ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize ⇒ InputGroup
Returns a new instance of InputGroup.
10 11 12 13 14 15 |
# File 'lib/midas-touch/input_group.rb', line 10 def initialize @inputs = Array.new @filters = Array.new @validations = Array.new @failures = Array.new end |
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
7 8 9 |
# File 'lib/midas-touch/input_group.rb', line 7 def filters @filters end |
#inputs ⇒ Object
Returns the value of attribute inputs.
6 7 8 |
# File 'lib/midas-touch/input_group.rb', line 6 def inputs @inputs end |
#validations ⇒ Object
Returns the value of attribute validations.
8 9 10 |
# File 'lib/midas-touch/input_group.rb', line 8 def validations @validations end |
Instance Method Details
#[](name) ⇒ Object
22 23 24 25 |
# File 'lib/midas-touch/input_group.rb', line 22 def [](name) input = input(name) input.value if input end |
#each(&block) ⇒ Object
68 69 70 |
# File 'lib/midas-touch/input_group.rb', line 68 def each(&block) to_h.each(&block) end |
#error_on?(name) ⇒ Boolean
54 55 56 |
# File 'lib/midas-touch/input_group.rb', line 54 def error_on?(name) @failures.include?(input(name)) end |
#fields ⇒ Object Also known as: keys
34 35 36 |
# File 'lib/midas-touch/input_group.rb', line 34 def fields @inputs.map(&:name) end |
#filter! ⇒ Object
27 28 29 30 31 32 |
# File 'lib/midas-touch/input_group.rb', line 27 def filter! @inputs.each do |input| filters.each { |filter| input.value = filter.call(input.value) } input.filter! end end |
#input(name) ⇒ Object
17 18 19 20 |
# File 'lib/midas-touch/input_group.rb', line 17 def input(name) name = name.to_sym @inputs.find { |input| input.name == name } end |
#invalidate_input(input_name) ⇒ Object
58 59 60 61 62 |
# File 'lib/midas-touch/input_group.rb', line 58 def invalidate_input(input_name) input = input(input_name) input.invalidate! @failures << input end |
#to_h ⇒ Object
64 65 66 |
# File 'lib/midas-touch/input_group.rb', line 64 def to_h Hash[@inputs.map { |input| [input.name, input.value] }] end |
#valid? ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/midas-touch/input_group.rb', line 39 def valid? @failures = Array.new @inputs.each do |input| validations.each do |rule| if !rule.call(input.value) @failures << input break end end @failures << input unless input.valid? end @failures.uniq! @failures.empty? end |