Class: PObject

Inherits:
Object
  • Object
show all
Defined in:
lib/pobject/pobject.rb,
lib/pobject/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, args = nil, &_block) ⇒ Object

Intercept any call to an unknown method, and try to load it from the store. If it does not exist in the store as well, then raise the usual error. This method will also be called when trying to assign to a non existing attribute. In this case, we will save it to the store.



19
20
21
22
23
24
25
26
# File 'lib/pobject/pobject.rb', line 19

def method_missing(method, args=nil, &_block)
  if method.to_s[-1] == "="
    set method.to_s.chomp('=').to_sym, args
  else
    content = get method
    content ? content : allow_missing? ? nil : super
  end
end

Class Method Details

.allow_missingObject

When ‘allow_missing` is called at the inheriting class level, then all calls to any missing attribute will return `nil` and supress the normal ruby behavior of raising a `NoMethodError`.



8
9
10
11
12
# File 'lib/pobject/pobject.rb', line 8

def self.allow_missing
  send :define_method, :allow_missing? do
    true
  end
end

Instance Method Details

#[](key) ⇒ Object

Allow hash-style access to any of our stored properties. This will not reload from the disk more than once.



39
40
41
# File 'lib/pobject/pobject.rb', line 39

def [](key)
  get key
end

#[]=(key, value) ⇒ Object

Allow hash-style assignment to new peoperties



44
45
46
# File 'lib/pobject/pobject.rb', line 44

def []=(key, value)
  set key, value
end

#attributesObject

Return attribute names, so we can print them and their values in ‘inspect`.



59
60
61
# File 'lib/pobject/pobject.rb', line 59

def attributes
  @attributes ||= attributes!
end

#attributes!Object

Return attribute names from the store. TODO: Maybe also merge with instance_variables. Pay attention to @s.



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pobject/pobject.rb', line 65

def attributes!
  result = []
  store.transaction do 
    if store_key
      result = store[store_key] || {}
      result = result.keys
    else
      result = store.roots
    end
  end
  result = result.select { |a| a.is_a? Symbol }
end

#inspectObject Also known as: to_s

Return a nice string when inspecting or printing. This will load values from the store if they were not already loaded.



50
51
52
53
54
# File 'lib/pobject/pobject.rb', line 50

def inspect
  properties = attributes.sort.map { |var| [var, get(var).to_s].join(":") }.join ', '
  properties = " #{properties}" unless properties.empty?
  "<#{self.class.name}#{properties}>"
end

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Mirror the method_missing behavior.

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/pobject/pobject.rb', line 29

def respond_to_missing?(method, include_private=false)
  if method.to_s[-1] == "="
    true
  else
    allow_missing? or !!get(method.to_sym)
  end
end