Module: Flexor::Plugins::Core::StoreMethods

Includes:
HashDelegation, MethodDispatch, Serialization, Vivification
Defined in:
lib/flexor/plugins/core.rb

Overview

Instance methods for the core Flexor data store.

Instance Method Summary collapse

Methods included from MethodDispatch

#respond_to_missing?

Methods included from Serialization

#encode_with, #init_with, #marshal_dump, #marshal_load

Methods included from HashDelegation

#empty?, #has_key?, #key?, #keys, #length, #size, #values

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Flexor::MethodDispatch

Instance Method Details

#==(other) ⇒ Object



114
115
116
117
118
119
120
121
# File 'lib/flexor/plugins/core.rb', line 114

def ==(other)
  case other
  in nil then nil?
  in Flexor then to_h == other.to_h
  in Hash then to_h == other
  else super
  end
end

#===(other) ⇒ Object



123
124
125
# File 'lib/flexor/plugins/core.rb', line 123

def ===(other)
  other.nil? ? nil? : super
end

#[](key) ⇒ Object



29
30
31
# File 'lib/flexor/plugins/core.rb', line 29

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

#[]=(key, value) ⇒ Object



33
34
35
# File 'lib/flexor/plugins/core.rb', line 33

def []=(key, value)
  @store[key] = vivify_value(value)
end

#clearObject



45
46
47
48
# File 'lib/flexor/plugins/core.rb', line 45

def clear
  @store.clear
  self
end

#deconstructObject



84
85
86
# File 'lib/flexor/plugins/core.rb', line 84

def deconstruct
  @store.values
end

#deconstruct_keys(keys) ⇒ Object



88
89
90
91
92
# File 'lib/flexor/plugins/core.rb', line 88

def deconstruct_keys(keys)
  return @store if keys.nil?

  @store.slice(*keys)
end

#delete(key) ⇒ Object



41
42
43
# File 'lib/flexor/plugins/core.rb', line 41

def delete(key)
  @store.delete(key)
end

#freezeObject



54
55
56
57
# File 'lib/flexor/plugins/core.rb', line 54

def freeze
  @store.freeze
  super
end

#initialize(hash = {}, root: true) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
# File 'lib/flexor/plugins/core.rb', line 17

def initialize(hash = {}, root: true)
  raise ArgumentError, "expected a Hash, got #{hash.class}" unless hash.is_a?(Hash)

  @root  = root
  @store = vivify(hash)
end

#initialize_copy(original) ⇒ Object



24
25
26
27
# File 'lib/flexor/plugins/core.rb', line 24

def initialize_copy(original)
  super
  @store = @store.dup
end

#inspectObject



77
78
79
80
81
82
# File 'lib/flexor/plugins/core.rb', line 77

def inspect
  return @store.inspect if @root
  return nil.inspect if @store.empty?

  @store.inspect
end

#merge(other) ⇒ Object



110
111
112
# File 'lib/flexor/plugins/core.rb', line 110

def merge(other)
  dup.merge!(other)
end

#merge!(other) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/flexor/plugins/core.rb', line 98

def merge!(other)
  other = other.to_h if other.is_a?(Flexor)
  other.each do |key, value|
    if value.is_a?(Hash) && self[key].is_a?(Flexor) && !self[key].nil?
      self[key].merge!(value)
    else
      self[key] = value
    end
  end
  self
end

#nil?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/flexor/plugins/core.rb', line 94

def nil?
  @store.empty?
end

#set_raw(key, value) ⇒ Object



37
38
39
# File 'lib/flexor/plugins/core.rb', line 37

def set_raw(key, value)
  @store[key] = value
end

#to_aryObject



50
51
52
# File 'lib/flexor/plugins/core.rb', line 50

def to_ary
  nil
end

#to_hObject



59
60
61
62
63
64
# File 'lib/flexor/plugins/core.rb', line 59

def to_h
  @store.each_with_object({}) do |(key, value), hash|
    result = recurse_to_h(value)
    hash[key] = result unless value.is_a?(Flexor) && result.nil?
  end
end

#to_jsonObject



66
67
68
69
# File 'lib/flexor/plugins/core.rb', line 66

def to_json(...)
  require "json"
  to_h.to_json(...)
end

#to_sObject



71
72
73
74
75
# File 'lib/flexor/plugins/core.rb', line 71

def to_s
  return "" if nil?

  @store.to_s
end