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
36
37
38
39
# 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



68
69
70
# File 'lib/eco/data/mapper.rb', line 68

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



49
50
51
# File 'lib/eco/data/mapper.rb', line 49

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

#empty?Boolean

Returns:

  • (Boolean)


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

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

#external?(value) ⇒ Boolean

@todo: review return true

Returns:

  • (Boolean)


89
90
91
92
93
94
# File 'lib/eco/data/mapper.rb', line 89

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)


97
98
99
100
101
# File 'lib/eco/data/mapper.rb', line 97

def include?(value)
  return true unless @source

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

#insensitive?Boolean

Returns:

  • (Boolean)


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

def insensitive?
  @insensitive
end

#internal?(value) ⇒ Boolean

@todo: review return true

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/eco/data/mapper.rb', line 81

def internal?(value)
  return true unless @source

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

#list(type = :internal) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/eco/data/mapper.rb', line 72

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)


104
105
106
107
108
# File 'lib/eco/data/mapper.rb', line 104

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

  value == to_internal(value)
end

#to_a(internal: @internal_order) ⇒ Object



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

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



117
118
119
120
121
122
# File 'lib/eco/data/mapper.rb', line 117

def to_external(value)
  return value unless @source

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

#to_internal(value) ⇒ Object



110
111
112
113
114
115
# File 'lib/eco/data/mapper.rb', line 110

def to_internal(value)
  return value unless @source

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

#to_json(internal: @internal_order) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/eco/data/mapper.rb', line 53

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

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