class Eco::Data::Mapper
Public Class Methods
new(array_of_arrays = [], internal: :last)
click to toggle source
it expects [[v1a, v1b], [v2a, v2b] …]
# File lib/eco/data/mapper.rb, line 6 def initialize (array_of_arrays = [], internal: :last) @internal_order = internal @source = array_of_arrays if @source # internal should be always last in @source @source = @source.map { |a| a.reverse } unless internal == :last # first declarations take priority @by_external = @source.reverse.to_h @by_internal = @source.reverse.map do |pair| pair.reverse end.to_h end end
Public Instance Methods
+(array_of_arrays)
click to toggle source
# File lib/eco/data/mapper.rb, line 39 def +(array_of_arrays) self.class.new(array_of_arrays + to_a, internal: @internal_order) end
as_json(internal: @internal_order)
click to toggle source
# File lib/eco/data/mapper.rb, line 22 def as_json(internal: @internal_order) to_a(internal: internal) end
external?(value)
click to toggle source
# File lib/eco/data/mapper.rb, line 55 def external?(value) return true if !@source @by_external.key?(value) end
include?(value)
click to toggle source
# File lib/eco/data/mapper.rb, line 60 def include?(value) return true if !@source internal?(value) || external?(value) end
internal?(value)
click to toggle source
# File lib/eco/data/mapper.rb, line 50 def internal?(value) return true if !@source @by_internal.key?(value) end
list(type = :internal)
click to toggle source
# File lib/eco/data/mapper.rb, line 43 def list(type = :internal) return [] if !@source @source.map do |pair| type == :internal ? pair.last : pair.first end.uniq end
to_a(internal: @internal_order)
click to toggle source
# File lib/eco/data/mapper.rb, line 33 def to_a(internal: @internal_order) @by_internal.map do |int, ext| internal == :last ? [ext, int] : [int, ext] end end
to_external(value)
click to toggle source
# File lib/eco/data/mapper.rb, line 70 def to_external(value) return value if !@source @by_internal[value] end
to_internal(value)
click to toggle source
# File lib/eco/data/mapper.rb, line 65 def to_internal(value) return value if !@source @by_external[value] end
to_json(internal: @internal_order)
click to toggle source
# File lib/eco/data/mapper.rb, line 26 def to_json(internal: @internal_order) content = as_json(internal: internal).map do |pair| " " + pair.to_json end.join(",\n") "[\n#{content}\n]" end