Class: Grandprix::Elements
- Inherits:
-
Object
- Object
- Grandprix::Elements
- Defined in:
- lib/grandprix/elements.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#+(other) ⇒ Object
other is also an Elements object.
- #==(other) ⇒ Object
-
#alongside(extra) ⇒ Object
extra is a hash from names to array of names.
-
#annotate(annotations) ⇒ Object
annotations is a hash of names to metadata.
-
#except(other) ⇒ Object
except is an array of names.
-
#initialize(array) ⇒ Elements
constructor
A new instance of Elements.
- #names ⇒ Object
-
#reorder(ordering) ⇒ Object
ordering is an array of names.
- #strings ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #underlying ⇒ Object
- #value_component(value) ⇒ Object
Constructor Details
#initialize(array) ⇒ Elements
Returns a new instance of Elements.
7 8 9 |
# File 'lib/grandprix/elements.rb', line 7 def initialize(array) @array = array end |
Class Method Details
.build(array) ⇒ Object
2 3 4 5 |
# File 'lib/grandprix/elements.rb', line 2 def self.build array elements = array.map {|element| element.split '=' } self.new(elements) end |
Instance Method Details
#+(other) ⇒ Object
other is also an Elements object
12 13 14 |
# File 'lib/grandprix/elements.rb', line 12 def +(other) self.class.new(@array + other.) end |
#==(other) ⇒ Object
80 81 82 83 |
# File 'lib/grandprix/elements.rb', line 80 def ==(other) return false unless other.respond_to?(:underlying) self. == other. end |
#alongside(extra) ⇒ Object
extra is a hash from names to array of names
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/grandprix/elements.rb', line 32 def alongside(extra) extra_pairs = extra.flat_map do |origin_name, destination_names| local_pair = find(origin_name) if local_pair and local_pair.size == 2 extra = local_pair[1] destination_names.map {|d| [d, extra] } elsif local_pair destination_names.map {|d| [d] } else [] end end #self + extra_elements self.class.new(@array + extra_pairs) end |
#annotate(annotations) ⇒ Object
annotations is a hash of names to metadata
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/grandprix/elements.rb', line 50 def annotate(annotations) def value_component(value) return nil if value.nil? return value if value.is_a? String return JSON.dump(value) end new_elements = .map do |element| name = element.first find(name).clone.tap do |row| annotation = value_component annotations[name] row[2] = annotation unless annotation.nil? end end self.class.new new_elements end |
#except(other) ⇒ Object
except is an array of names
17 18 19 |
# File 'lib/grandprix/elements.rb', line 17 def except(other) self.class.new(@array.reject {|e| other.include? e.first}) end |
#names ⇒ Object
72 73 74 |
# File 'lib/grandprix/elements.rb', line 72 def names @array.map &:first end |
#reorder(ordering) ⇒ Object
ordering is an array of names
22 23 24 25 26 27 28 29 |
# File 'lib/grandprix/elements.rb', line 22 def reorder(ordering) names_in_order = ordering & names new_array = names_in_order.map do |name| find(name) end self.class.new(new_array) end |
#strings ⇒ Object
68 69 70 |
# File 'lib/grandprix/elements.rb', line 68 def strings .map {|e| e.join("=") } end |
#to_a ⇒ Object
76 77 78 |
# File 'lib/grandprix/elements.rb', line 76 def to_a names end |
#to_s ⇒ Object
85 86 87 |
# File 'lib/grandprix/elements.rb', line 85 def to_s "<Elements #{self.strings.inspect}>" end |
#underlying ⇒ Object
89 90 91 |
# File 'lib/grandprix/elements.rb', line 89 def @array end |
#value_component(value) ⇒ Object
51 52 53 54 55 |
# File 'lib/grandprix/elements.rb', line 51 def value_component(value) return nil if value.nil? return value if value.is_a? String return JSON.dump(value) end |