Module: Proxima::Attributes::ClassMethods

Defined in:
lib/proxima/attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(attribute, klass = nil, json_path = nil, params = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/proxima/attributes.rb', line 38

def attribute(attribute, klass = nil, json_path = nil, params = nil)
  params ||= json_path if json_path.is_a? Hash
  params ||= klass     if klass.is_a? Hash
  params ||= {}

  params[:klass]     ||= klass     if klass.is_a? Class
  params[:json_path] ||= json_path if json_path.is_a? String
  params[:json_path] ||= klass     if klass.is_a? String
  params[:json_path] ||= attribute.to_s

  # Create attribute accessors
  attr_reader attribute
  define_method("#{attribute}=") do |value|
    self.persisted = false
    attribute_will_change! attribute
    self.instance_variable_set "@#{attribute}", value
    Proxima.watch value do
      attribute_will_change! attribute
    end
  end

  # Create attribute? methods
  define_method "#{attribute}?" do
    self.instance_variable_get("@#{attribute}") != nil
  end

  # Create suffixed/prefixed attribute methods
  self.define_attribute_method attribute

  self.attributes[attribute] = params
end

#attributesObject



34
35
36
# File 'lib/proxima/attributes.rb', line 34

def attributes
  @attributes ||= {}
end