Class: Cts::Mpx::Fields

Inherits:
Object
  • Object
show all
Includes:
Creatable, Enumerable
Defined in:
lib/cts/mpx/fields.rb

Overview

Enumerable collection that can store an entry field in it

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVoid

Reset the field array to a blank state



73
74
75
# File 'lib/cts/mpx/fields.rb', line 73

def initialize
  reset
end

Instance Attribute Details

#collection storage for the individual field(storage) ⇒ Field[]

Returns:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cts/mpx/fields.rb', line 6

class Fields
  include Enumerable
  include Creatable

  attribute name: 'collection', kind_of: Array

  # Create a new fields collection from a data hash
  # @param [Hash] data raw fields to add
  # @param [Hash] xmlns namespace of the fields
  # @raise [ArgumentError] if :data or :xmlns are not provided
  # @return [Fields] a new fields collection
  def self.create_from_data(data: nil, xmlns: nil)
    Driver::Helpers.required_arguments([:data], binding)
    obj = new
    obj.parse(data: data, xmlns: xmlns)
    obj
  end

  # Addressable method, indexed by field name
  # @param [String] key name of the field
  # @return [Self.collection,Field,nil] Can return the collection, a single field, or nil if nothing found
  def [](key = nil)
    return @collection unless key

    result = @collection.find { |f| f.name == key }
    return result.value if result

    nil
  end

  # Addresable set method, indexed by field name
  # @note will create a new copy if it is not found in the collection
  # @param [String] key name of the field
  # @param [Object] value value of the field
  # @param [Hash] xmlns namespace of the field
  # @example to include xmlns, you need to use the long format of this method
  #    fields.[]= 'id', 'value', xmlns: {}
  # @return [Void]
  def []=(key, value, xmlns: nil)
    existing_field = find { |f| f.name == key }
    if existing_field
      existing_field.value = value
    else
      add Field.create name: key, value: value, xmlns: xmlns
    end
  end

  # Add a field object to the collection
  # @param [Field] field instantiated Field to include
  # @raise [ArgumentError] if field is not a Field
  # @return [Self]
  def add(field)
    return self if @collection.include? field

    Driver::Exceptions.raise_unless_argument_error? field, Field
    @collection.push field
    self
  end

  # Iterator method for self
  # @return [Field] next object in the list
  def each
    @collection.each { |c| yield c }
  end

  # Reset the field array to a blank state
  # @return [Void]
  def initialize
    reset
  end

  # Parse two hashes into a field array
  # @note this will also set the collection
  # @param [Hash] xmlns namespace
  # @param [Hash] data fields in hash form
  # @raise [ArgumentError] if xmlns is not a Hash
  # @return [Field[]] returns a collection of fields
  def parse(xmlns: nil, data: nil)
    Driver::Exceptions.raise_unless_argument_error? data, Hash

    data.delete :service
    data.delete :endpoint
    reset
    @collection = data.map { |k, v| Field.create(name: k.to_s, value: v, xmlns: xmlns) }
  end

  # Remove a field object from the collection
  # @param [String] name instantiated Field to remove
  # @return [Self]
  def remove(name)
    @collection.delete_if { |f| f.name == name }
  end

  # Reset the field array to a blank state
  # @return [Void]
  def reset
    @collection = []
  end

  # return the fields as a hash
  # @return [Hash] key is name, value is value
  def to_h
    h = {}
    each { |f| h.store f.name, f.value }
    h
  end

  # Return the cumulative namespace of all Field's in the collection
  # @return [Hash] key is the namespace key, value is the value
  def xmlns
    a = collection.map(&:xmlns).uniq
    a.delete nil
    h = {}
    a.each { |e| h.merge! e }
    h
  end
end

Class Method Details

.create_from_data(data: nil, xmlns: nil) ⇒ Fields

Create a new fields collection from a data hash

Parameters:

  • data (Hash) (defaults to: nil)

    raw fields to add

  • xmlns (Hash) (defaults to: nil)

    namespace of the fields

Returns:

  • (Fields)

    a new fields collection

Raises:

  • (ArgumentError)

    if :data or :xmlns are not provided



17
18
19
20
21
22
# File 'lib/cts/mpx/fields.rb', line 17

def self.create_from_data(data: nil, xmlns: nil)
  Driver::Helpers.required_arguments([:data], binding)
  obj = new
  obj.parse(data: data, xmlns: xmlns)
  obj
end

Instance Method Details

#[](key = nil) ⇒ Self.collection, ...

Addressable method, indexed by field name

Parameters:

  • key (String) (defaults to: nil)

    name of the field

Returns:

  • (Self.collection, Field, nil)

    Can return the collection, a single field, or nil if nothing found



27
28
29
30
31
32
33
34
# File 'lib/cts/mpx/fields.rb', line 27

def [](key = nil)
  return @collection unless key

  result = @collection.find { |f| f.name == key }
  return result.value if result

  nil
end

#[]=(key, value, xmlns: nil) ⇒ Void

Note:

will create a new copy if it is not found in the collection

Addresable set method, indexed by field name

Examples:

to include xmlns, you need to use the long format of this method

fields.[]= 'id', 'value', xmlns: {}

Parameters:

  • key (String)

    name of the field

  • value (Object)

    value of the field

  • xmlns (Hash) (defaults to: nil)

    namespace of the field

Returns:

  • (Void)


44
45
46
47
48
49
50
51
# File 'lib/cts/mpx/fields.rb', line 44

def []=(key, value, xmlns: nil)
  existing_field = find { |f| f.name == key }
  if existing_field
    existing_field.value = value
  else
    add Field.create name: key, value: value, xmlns: xmlns
  end
end

#add(field) ⇒ Self

Add a field object to the collection

Parameters:

  • field (Field)

    instantiated Field to include

Returns:

  • (Self)

Raises:

  • (ArgumentError)

    if field is not a Field



57
58
59
60
61
62
63
# File 'lib/cts/mpx/fields.rb', line 57

def add(field)
  return self if @collection.include? field

  Driver::Exceptions.raise_unless_argument_error? field, Field
  @collection.push field
  self
end

#eachField

Iterator method for self

Returns:

  • (Field)

    next object in the list



67
68
69
# File 'lib/cts/mpx/fields.rb', line 67

def each
  @collection.each { |c| yield c }
end

#parse(xmlns: nil, data: nil) ⇒ Field[]

Note:

this will also set the collection

Parse two hashes into a field array

Parameters:

  • xmlns (Hash) (defaults to: nil)

    namespace

  • data (Hash) (defaults to: nil)

    fields in hash form

Returns:

  • (Field[])

    returns a collection of fields

Raises:

  • (ArgumentError)

    if xmlns is not a Hash



83
84
85
86
87
88
89
90
# File 'lib/cts/mpx/fields.rb', line 83

def parse(xmlns: nil, data: nil)
  Driver::Exceptions.raise_unless_argument_error? data, Hash

  data.delete :service
  data.delete :endpoint
  reset
  @collection = data.map { |k, v| Field.create(name: k.to_s, value: v, xmlns: xmlns) }
end

#remove(name) ⇒ Self

Remove a field object from the collection

Parameters:

  • name (String)

    instantiated Field to remove

Returns:

  • (Self)


95
96
97
# File 'lib/cts/mpx/fields.rb', line 95

def remove(name)
  @collection.delete_if { |f| f.name == name }
end

#resetVoid

Reset the field array to a blank state

Returns:

  • (Void)


101
102
103
# File 'lib/cts/mpx/fields.rb', line 101

def reset
  @collection = []
end

#to_hHash

return the fields as a hash

Returns:

  • (Hash)

    key is name, value is value



107
108
109
110
111
# File 'lib/cts/mpx/fields.rb', line 107

def to_h
  h = {}
  each { |f| h.store f.name, f.value }
  h
end

#xmlnsHash

Return the cumulative namespace of all Field’s in the collection

Returns:

  • (Hash)

    key is the namespace key, value is the value



115
116
117
118
119
120
121
# File 'lib/cts/mpx/fields.rb', line 115

def xmlns
  a = collection.map(&:xmlns).uniq
  a.delete nil
  h = {}
  a.each { |e| h.merge! e }
  h
end