Class: Hippo::TransactionSets::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/hippo/transaction_sets/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options)
  @identified_by  = options.delete(:identified_by)              || {}
  @conditions     = options.delete(:parent_context_conditions)  || {}
  @maximum        = options.delete(:maximum)                    || 1
  @klass          = options.delete(:klass)
  @sequence       = options.delete(:sequence)

  @options        = options
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



3
4
5
# File 'lib/hippo/transaction_sets/component.rb', line 3

def conditions
  @conditions
end

#identified_byObject (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

#klassObject (readonly)

Returns the value of attribute klass.



3
4
5
# File 'lib/hippo/transaction_sets/component.rb', line 3

def klass
  @klass
end

#maximumObject (readonly)

Returns the value of attribute maximum.



3
4
5
# File 'lib/hippo/transaction_sets/component.rb', line 3

def maximum
  @maximum
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/hippo/transaction_sets/component.rb', line 3

def options
  @options
end

#sequenceObject (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

Returns:

  • (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

#identifierObject



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

Returns:

  • (Boolean)


15
16
17
# File 'lib/hippo/transaction_sets/component.rb', line 15

def repeating?
  @maximum > 1
end

#segment?Boolean

Returns:

  • (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?

Returns:

  • (Boolean)


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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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