Class: MarkMapper::OptionsHash

Inherits:
Object
  • Object
show all
Defined in:
lib/mark_mapper/options_hash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}, options = {}) ⇒ OptionsHash

Public


16
17
18
19
20
# File 'lib/mark_mapper/options_hash.rb', line 16

def initialize(hash={}, options={})
  @source = {}
  @options = options
  hash.each { |key, value| self[key] = value }
end

Instance Attribute Details

#optionsObject (readonly)

Private: The Hash that stores instance options


13
14
15
# File 'lib/mark_mapper/options_hash.rb', line 13

def options
  @options
end

#sourceObject (readonly)

Private: The Hash that stores the query options


10
11
12
# File 'lib/mark_mapper/options_hash.rb', line 10

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object

Public


47
48
49
# File 'lib/mark_mapper/options_hash.rb', line 47

def ==(other)
  @source == other.source
end

#[](key) ⇒ Object

Public


31
32
33
# File 'lib/mark_mapper/options_hash.rb', line 31

def [](key)
  @source[key]
end

#[]=(key, value) ⇒ Object

Public


36
37
38
39
# File 'lib/mark_mapper/options_hash.rb', line 36

def []=(key, value)
  key = normalized_key(key)
  @source[key] = normalized_value(key, value)
end

#fields?Boolean

Public

Returns:


57
58
59
# File 'lib/mark_mapper/options_hash.rb', line 57

def fields?
  !self[:fields].nil?
end

#initialize_copy(original) ⇒ Object


22
23
24
25
26
27
28
# File 'lib/mark_mapper/options_hash.rb', line 22

def initialize_copy(original)
  super
  @source = @source.dup
  @source.each do |key, value|
    self[key] = value.clone if value.duplicable?
  end
end

#key_normalizerObject

Private


83
84
85
86
87
88
89
90
91
92
# File 'lib/mark_mapper/options_hash.rb', line 83

def key_normalizer
  @key_normalizer ||= @options.fetch(:key_normalizer) {
    Normalizers::HashKey.new({
      :order  => :sort,
      :select => :fields,
      :offset => :skip,
      :id     => :_id,
    })
  }
end

#keysObject

Public


42
43
44
# File 'lib/mark_mapper/options_hash.rb', line 42

def keys
  @source.keys
end

#merge(other) ⇒ Object

Public


62
63
64
# File 'lib/mark_mapper/options_hash.rb', line 62

def merge(other)
  self.class.new(to_hash.merge(other.to_hash))
end

#merge!(other) ⇒ Object

Public


67
68
69
70
# File 'lib/mark_mapper/options_hash.rb', line 67

def merge!(other)
  other.to_hash.each { |key, value| self[key] = value }
  self
end

#normalized_key(key) ⇒ Object

Private


73
74
75
# File 'lib/mark_mapper/options_hash.rb', line 73

def normalized_key(key)
  key_normalizer.call(key)
end

#normalized_value(key, value) ⇒ Object

Private


78
79
80
# File 'lib/mark_mapper/options_hash.rb', line 78

def normalized_value(key, value)
  value_normalizer.call(key, value)
end

#to_hashObject

Public


52
53
54
# File 'lib/mark_mapper/options_hash.rb', line 52

def to_hash
  @source
end

#value_normalizerObject

Private


95
96
97
98
99
100
101
# File 'lib/mark_mapper/options_hash.rb', line 95

def value_normalizer
  @value_normalizer ||= @options.fetch(:value_normalizer) {
    Normalizers::OptionsHashValue.new({
      :key_normalizer => key_normalizer,
    })
  }
end