Class: CouchTomato::Persistence::SimpleProperty

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_tomato/persistence/simple_property.rb

Overview

:nodoc:

Constant Summary collapse

JSON_TYPES =
[String, Integer, Hash, Array, Fixnum, Float]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_clazz, name, options = {}) ⇒ SimpleProperty

Returns a new instance of SimpleProperty.



7
8
9
10
11
12
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
48
49
50
51
52
53
54
55
56
57
# File 'lib/couch_tomato/persistence/simple_property.rb', line 7

def initialize(owner_clazz, name, options = {})
  if JSON_TYPES.include?(options[:type])
    raise "#{options[:type]} is a native JSON type, only custom types should be specified"
  end

  if options[:type].kind_of?(Array) && options[:type].empty?
    raise "property defined with `:type => []` but expected `:type => [SomePersistableType]`"
  end

  self.name = name
  self.type = options[:type]
  owner_clazz.class_eval do
    attr_reader name, "#{name}_was"

    def initialize(attributes = {})
      super attributes
      # assign_attribute_copies_for_dirty_tracking
    end

    # def assign_attribute_copies_for_dirty_tracking
    #   attributes.each do |name, value|
    #     self.instance_variable_set("@#{name}_was", clone_attribute(value))
    #   end if attributes
    # end
    # private :assign_attribute_copies_for_dirty_tracking

    def clone_attribute(value)
      if [Bignum, Fixnum, Symbol, TrueClass, FalseClass, NilClass, Float].include?(value.class)
        value
      else
        value.clone
      end
    end

    define_method "#{name}=" do |value|
      self.instance_variable_set("@#{name}", value)
    end

    define_method "#{name}?" do
      !self.send(name).nil? && !self.send(name).try(:blank?)
    end

    define_method "#{name}_changed?" do
      !self.instance_variable_get("@#{name}_not_changed") && self.send(name) != self.send("#{name}_was")
    end

    define_method "#{name}_not_changed" do
      self.instance_variable_set("@#{name}_not_changed", true)
    end
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/couch_tomato/persistence/simple_property.rb', line 4

def name
  @name
end

#typeObject

Returns the value of attribute type.



4
5
6
# File 'lib/couch_tomato/persistence/simple_property.rb', line 4

def type
  @type
end

Instance Method Details

#build(object, json) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/couch_tomato/persistence/simple_property.rb', line 59

def build(object, json)
  value = json[name.to_s]
  value = json[name.to_sym] if value.nil?

  if type.kind_of? Array
    typecasted_value = []
    value.each do |val|
      el = type[0].json_create val
      typecasted_value << el
    end
  else
    typecasted_value = if type
                         type.json_create value
                       else
                         value
                       end
  end

  object.send "#{name}=", typecasted_value
end

#dirty?(object) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/couch_tomato/persistence/simple_property.rb', line 80

def dirty?(object)
  object.send("#{name}_changed?")
end

#serialize(json, object) ⇒ Object

def save(object)

end

def destroy(object)

end


92
93
94
# File 'lib/couch_tomato/persistence/simple_property.rb', line 92

def serialize(json, object)
  json[name] = object.send name
end