Class: Plucky::OptionsHash

Inherits:
Object show all
Defined in:
lib/plucky/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/plucky/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/plucky/options_hash.rb', line 13

def options
  @options
end

#sourceObject (readonly)

Private: The Hash that stores the query options



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

def source
  @source
end

Instance Method Details

#==(other) ⇒ Object

Public



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

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

#[](key) ⇒ Object

Public



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

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

#[]=(key, value) ⇒ Object

Public



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

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

#fields?Boolean

Public

Returns:

  • (Boolean)


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

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

#initialize_copy(original) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/plucky/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
93
# File 'lib/plucky/options_hash.rb', line 83

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

#keysObject

Public



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

def keys
  @source.keys
end

#merge(other) ⇒ Object

Public



62
63
64
# File 'lib/plucky/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/plucky/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/plucky/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/plucky/options_hash.rb', line 78

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

#to_hashObject

Public



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

def to_hash
  @source
end

#value_normalizerObject

Private



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

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