Class: Hippo::TransactionSets::Component
- Inherits:
-
Object
- Object
- Hippo::TransactionSets::Component
- Defined in:
- lib/hippo/transaction_sets/component.rb
Instance Attribute Summary collapse
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#identified_by ⇒ Object
readonly
Returns the value of attribute identified_by.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#maximum ⇒ Object
readonly
Returns the value of attribute maximum.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#sequence ⇒ Object
readonly
Returns the value of attribute sequence.
Instance Method Summary collapse
- #conditions_match?(instance, segment) ⇒ Boolean
- #identifier ⇒ Object
-
#initialize(options) ⇒ Component
constructor
A new instance of Component.
- #initialize_component(parent) ⇒ Object
- #populate_component(component, defaults = nil) ⇒ Object
- #repeating? ⇒ Boolean
- #segment? ⇒ Boolean
- #transaction_set? ⇒ Boolean (also: #loop?)
- #valid?(segment) ⇒ Boolean
- #valid_entry_segment?(segment) ⇒ Boolean
- #valid_segment?(segment) ⇒ Boolean
Constructor Details
#initialize(options) ⇒ Component
Returns a new instance of Component.
5 6 7 8 9 10 11 12 13 |
# File 'lib/hippo/transaction_sets/component.rb', line 5 def initialize() @identified_by = .delete(:identified_by) || {} @conditions = .delete(:parent_context_conditions) || {} @maximum = .delete(:maximum) || 1 @klass = .delete(:klass) @sequence = .delete(:sequence) @options = end |
Instance Attribute Details
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
3 4 5 |
# File 'lib/hippo/transaction_sets/component.rb', line 3 def conditions @conditions end |
#identified_by ⇒ Object (readonly)
Returns the value of attribute identified_by.
3 4 5 |
# File 'lib/hippo/transaction_sets/component.rb', line 3 def identified_by @identified_by end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/hippo/transaction_sets/component.rb', line 3 def klass @klass end |
#maximum ⇒ Object (readonly)
Returns the value of attribute maximum.
3 4 5 |
# File 'lib/hippo/transaction_sets/component.rb', line 3 def maximum @maximum end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/hippo/transaction_sets/component.rb', line 3 def @options end |
#sequence ⇒ Object (readonly)
Returns the value of attribute sequence.
3 4 5 |
# File 'lib/hippo/transaction_sets/component.rb', line 3 def sequence @sequence end |
Instance Method Details
#conditions_match?(instance, segment) ⇒ Boolean
86 87 88 89 90 91 92 93 94 |
# File 'lib/hippo/transaction_sets/component.rb', line 86 def conditions_match?(instance, segment) if conditions.empty? true else conditions.all? do |method, expected| Array(expected).include?(instance.instance_eval("self." + method)) end end end |
#identifier ⇒ Object
28 29 30 |
# File 'lib/hippo/transaction_sets/component.rb', line 28 def identifier @klass.identifier end |
#initialize_component(parent) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/hippo/transaction_sets/component.rb', line 52 def initialize_component(parent) if repeating? RepeatingComponent.new(self, parent) else populate_component(@klass.new(:parent => parent)) end end |
#populate_component(component, defaults = nil) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/hippo/transaction_sets/component.rb', line 32 def populate_component(component, defaults = nil) defaults ||= identified_by defaults.each do |key, value| value = Array(value) if key =~ /(\w+)\.(.+)/ next_component, next_component_value = component.send($1.to_sym), {$2 => value} populate_component(next_component, next_component_value) else next if value.length > 1 component.send((key + '=').to_sym, value.first) end end component end |
#repeating? ⇒ Boolean
15 16 17 |
# File 'lib/hippo/transaction_sets/component.rb', line 15 def repeating? @maximum > 1 end |
#segment? ⇒ Boolean
24 25 26 |
# File 'lib/hippo/transaction_sets/component.rb', line 24 def segment? @klass.ancestors.include? Hippo::Segments::Base end |
#transaction_set? ⇒ Boolean Also known as: loop?
19 20 21 |
# File 'lib/hippo/transaction_sets/component.rb', line 19 def transaction_set? @klass.ancestors.include? Hippo::TransactionSets::Base end |
#valid?(segment) ⇒ Boolean
60 61 62 63 64 65 66 |
# File 'lib/hippo/transaction_sets/component.rb', line 60 def valid?(segment) if klass.ancestors.include? Hippo::Segments::Base valid_segment?(segment) else valid_entry_segment?(segment) end end |
#valid_entry_segment?(segment) ⇒ Boolean
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/hippo/transaction_sets/component.rb', line 72 def valid_entry_segment?(segment) if identified_by.empty? initial_segment_id = @klass.components.first.identifier segment.identifier == initial_segment_id else path, value = identified_by.first # TODO: handle arbitrary depth in loop identified_by parsing segment_id, field_name = path.split('.') segment.identifier == segment_id && Array(value).include?(segment.send(field_name)) end end |
#valid_segment?(segment) ⇒ Boolean
68 69 70 |
# File 'lib/hippo/transaction_sets/component.rb', line 68 def valid_segment?(segment) segment.identifier == identifier && identified_by.all? {|k, v| Array(v).include?(segment.send(k.to_sym))} end |