Module: Intellihash::Mixins

Defined in:
lib/intellihash/mixins.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs, &block) ⇒ Object (private)



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/intellihash/mixins.rb', line 53

def method_missing(method_name, *args, **kwargs, &block)
  super unless respond_to?(:is_intelligent?) && is_intelligent?

  if method_name[-1] == '='
    send(:store, method_name[0, method_name.size - 1].send(key_store_as), args.first)
  else
    format_method = key_retrieve_from(kwargs)
    case format_method
    when :any then fetch_where_present(method_name)
    else send(:[], method_name.send(format_method))
    end
  end
end

Instance Method Details

#default_formatObject



25
26
27
28
29
# File 'lib/intellihash/mixins.rb', line 25

def default_format
  return @default_format if frozen?

  @default_format ||= Intellihash.configuration.default_format
end

#default_format=(other) ⇒ Object



31
32
33
# File 'lib/intellihash/mixins.rb', line 31

def default_format=(other)
  @default_format = FORMATTER.member?(other) ? other : FORMATTER[:symbol]
end

#freezeObject



35
36
37
38
39
40
41
# File 'lib/intellihash/mixins.rb', line 35

def freeze
  # Touch these attributes if it hasn't already happened, ensure they're not nil
  intelligent
  default_format

  super
end

#intelligentObject



5
6
7
8
9
# File 'lib/intellihash/mixins.rb', line 5

def intelligent
  return @intelligent if frozen?

  @intelligent = @intelligent.nil? ? Intellihash.configuration.intelligent_by_default : @intelligent
end

#is_intelligent=(value) ⇒ Object



11
12
13
14
# File 'lib/intellihash/mixins.rb', line 11

def is_intelligent=(value)
  # Ensure this is a boolean
  @intelligent = value == true
end

#is_intelligent?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/intellihash/mixins.rb', line 21

def is_intelligent?
  intelligent
end

#to_intellihashObject



16
17
18
19
# File 'lib/intellihash/mixins.rb', line 16

def to_intellihash
  @intelligent = true
  self
end