Class: HyperResource::Attributes

Inherits:
Hash
  • Object
show all
Defined in:
lib/hyper_resource/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource = nil) ⇒ Attributes

Returns a new instance of Attributes.



6
7
8
# File 'lib/hyper_resource/attributes.rb', line 6

def initialize(resource=nil) # @private
  self._resource = resource || HyperResource.new
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/hyper_resource/attributes.rb', line 78

def method_missing(method, *args) # @private
  method = method.to_s
  if has_key?(method)
    self[method]
  elsif method[-1,1] == '='
    self[method[0..-2]] = args.first
  else
    raise NoMethodError, "undefined method `#{method}' for #{self.inspect}"
  end
end

Instance Attribute Details

#_resourceObject



4
5
6
# File 'lib/hyper_resource/attributes.rb', line 4

def _resource
  @_resource
end

Instance Method Details

#[](key) ⇒ Object



72
73
74
75
76
# File 'lib/hyper_resource/attributes.rb', line 72

def [](key) # @private
  return super(key.to_s) if self.has_key?(key.to_s)
  return super(key.to_sym) if self.has_key?(key.to_sym)
  nil
end

#[]=(attr, value) ⇒ Object



66
67
68
69
70
# File 'lib/hyper_resource/attributes.rb', line 66

def []=(attr, value) # @private
  return self[attr] if self.has_key?(attr.to_s) && self[attr] == value
  _hr_mark_changed(attr) 
  super(attr.to_s, value)
end

#_hr_clear_changedObject



89
90
91
# File 'lib/hyper_resource/attributes.rb', line 89

def _hr_clear_changed # @private
  @_hr_changed = nil
end

#_hr_create_methods!(opts = {}) ⇒ Object

Creates accessor methods in self.class and self._resource.class. Protects against method creation into HyperResource::Attributes and HyperResource classes. Just subclasses, please!



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hyper_resource/attributes.rb', line 13

def _hr_create_methods!(opts={}) # @private
  return if self.class.to_s == 'HyperResource::Attributes'
  return if self._resource.class.to_s == 'HyperResource'
  return if self.class.send(
    :class_variable_defined?, :@@_hr_created_attributes_methods)

  self.keys.each do |attr|
    attr_sym = attr.to_sym
    attr_eq_sym = "#{attr}=".to_sym

    self.class.send(:define_method, attr_sym) do
      self[attr]
    end
    self.class.send(:define_method, attr_eq_sym) do |val|
      self[attr] = val
    end

    ## Don't stomp on _resource's methods
    unless _resource.respond_to?(attr_sym)
      _resource.class.send(:define_method, attr_sym) do
        attributes.send(attr_sym)
      end
    end
    unless _resource.respond_to?(attr_eq_sym)
      _resource.class.send(:define_method, attr_eq_sym) do |val|
        attributes.send(attr_eq_sym, val)
      end
    end
  end

  ## This is a good time to mark this object as not-changed
  _hr_clear_changed

  self.class.send(:class_variable_set, :@@_hr_created_attributes_methods, true) 
end

#_hr_mark_changed(attr, is_changed = true) ⇒ Object



93
94
95
96
97
# File 'lib/hyper_resource/attributes.rb', line 93

def _hr_mark_changed(attr, is_changed=true) # @private
  attr = attr.to_sym
  @_hr_changed ||= Hash.new(false)
  @_hr_changed[attr] = is_changed
end

#changed?(attr = nil) ⇒ Boolean

Returns true if the given attribute has been changed since creation time, false otherwise. If no attribute is given, return whether any attributes have been changed.

Returns:

  • (Boolean)


53
54
55
56
57
# File 'lib/hyper_resource/attributes.rb', line 53

def changed?(attr=nil)
  @_hr_changed ||= Hash.new(false)
  return @_hr_changed[attr.to_sym] if attr
  return @_hr_changed.keys.count > 0
end

#changed_attributesObject

Returns a hash of the attributes and values which have been changed since creation time.



61
62
63
64
# File 'lib/hyper_resource/attributes.rb', line 61

def changed_attributes
  return {} if @_hr_changed.nil?
  @_hr_changed.select{|k,v| v}.keys.inject({}) {|h,k| h[k]=self[k]; h}
end