Class: Eco::Data::Mapper

Inherits:
Object show all
Defined in:
lib/eco/data/mapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(array_of_arrays = [], internal: :last, insensitive: false) ⇒ Mapper

it expects [[v1a, v1b], [v2a, v2b] ...]



5
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
# File 'lib/eco/data/mapper.rb', line 5

def initialize(array_of_arrays = [], internal: :last, insensitive: false)
  @internal_order = internal
  @source         = array_of_arrays
  @insensitive    = insensitive

  return unless @source

  # internal should be always last in @source
  @source = @source.map(&:reverse) unless internal == :last

  # first declarations take priority
  @by_external = @source.reverse.to_h.tap do |h_data|
    next unless insensitive?

    h_data.dup.each do |key, value|
      next if h_data.key?(key.downcase)

      h_data[key.downcase] = value
    end
  end

  @by_internal = @source.reverse.to_h(&:reverse).tap do |h_data|
    next unless insensitive?

    h_data.dup.each do |key, value|
      next if h_data.key?(key.downcase)

      h_data[key.downcase] = value
    end
  end
end

Instance Method Details

#+(array_of_arrays) ⇒ Object

rubocop:disable Naming/BinaryOperatorParameterName



64
65
66
# File 'lib/eco/data/mapper.rb', line 64

def +(array_of_arrays) # rubocop:disable Naming/BinaryOperatorParameterName
  self.class.new(array_of_arrays + to_a, internal: @internal_order)
end

#as_json(internal: @internal_order) ⇒ Object



45
46
47
# File 'lib/eco/data/mapper.rb', line 45

def as_json(internal: @internal_order)
  to_a(internal: internal)
end

#empty?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/eco/data/mapper.rb', line 37

def empty?
  @by_external.empty? && @by_internal.empty?
end

#external?(value) ⇒ Boolean

@todo: review return true

Returns:

  • (Boolean)


85
86
87
88
89
90
# File 'lib/eco/data/mapper.rb', line 85

def external?(value)
  return true unless @source

  value = value.downcase if insensitive?
  @by_external.key?(value)
end

#include?(value) ⇒ Boolean

@todo: review return true

Returns:

  • (Boolean)


93
94
95
96
97
# File 'lib/eco/data/mapper.rb', line 93

def include?(value)
  return true unless @source

  internal?(value) || external?(value)
end

#insensitive?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/eco/data/mapper.rb', line 41

def insensitive?
  @insensitive
end

#internal?(value) ⇒ Boolean

@todo: review return true

Returns:

  • (Boolean)


77
78
79
80
81
82
# File 'lib/eco/data/mapper.rb', line 77

def internal?(value)
  return true unless @source

  value = value.downcase if insensitive?
  @by_internal.key?(value)
end

#list(type = :internal) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/eco/data/mapper.rb', line 68

def list(type = :internal)
  return [] unless @source

  @source.map do |pair|
    type == :internal ? pair.last : pair.first
  end.uniq
end

#self_mapped?(value) ⇒ Boolean

Whether value maps to itself

Returns:

  • (Boolean)


100
101
102
103
104
# File 'lib/eco/data/mapper.rb', line 100

def self_mapped?(value)
  return false unless include?(value)

  value == to_internal(value)
end

#to_a(internal: @internal_order) ⇒ Object



57
58
59
60
61
62
# File 'lib/eco/data/mapper.rb', line 57

def to_a(internal: @internal_order)
  src_dup = @source.map {|pair| pair[0..]}
  return src_dup if internal == :last

  src_dup.map(&:reverse)
end

#to_external(value) ⇒ Object



113
114
115
116
117
118
# File 'lib/eco/data/mapper.rb', line 113

def to_external(value)
  return value unless @source

  value = value.downcase if insensitive?
  @by_internal[value]
end

#to_internal(value) ⇒ Object



106
107
108
109
110
111
# File 'lib/eco/data/mapper.rb', line 106

def to_internal(value)
  return value unless @source

  value = value.downcase if insensitive?
  @by_external[value]
end

#to_json(internal: @internal_order) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/eco/data/mapper.rb', line 49

def to_json(internal: @internal_order)
  content = as_json(internal: internal).map do |pair|
    "  #{pair.to_json}"
  end.join(",\n")

  "[\n#{content}\n]"
end