Module: Dolly::PropertyManager

Included in:
Document
Defined in:
lib/dolly/property_manager.rb

Instance Method Summary collapse

Instance Method Details

#assign_identity_properties(opts = {}) ⇒ Object



47
48
49
50
# File 'lib/dolly/property_manager.rb', line 47

def assign_identity_properties(opts = {})
  id_presence = opts[:id] || opts[:_id] || opts['id'] || opts['_id']
  self.id = id_presence if id_presence
end

#assign_rev_properties(opts = {}) ⇒ Object



52
53
54
55
# File 'lib/dolly/property_manager.rb', line 52

def assign_rev_properties(opts = {})
  rev_presence = opts[:rev] || opts [:_rev] || opts['rev'] || opts['_rev']
  self.rev = rev_presence if rev_presence
end

#build_property(attributes) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/dolly/property_manager.rb', line 5

def build_property(attributes)
  assign_identity_properties(attributes)
  assign_rev_properties(attributes)

  lambda do |property|
    name = property.key
    next unless doc[name].nil?

    write_attribute(name, attributes[name])
  end
end

#propertiesObject



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

def properties
  self.class.properties
end

#set_property_value(key, value) ⇒ Object



43
44
45
# File 'lib/dolly/property_manager.rb', line 43

def set_property_value(key, value)
  properties[key].cast_value(value)
end

#update_attributeObject



17
18
19
20
21
22
23
# File 'lib/dolly/property_manager.rb', line 17

def update_attribute
  lambda do |(key, value)|
    raise InvalidProperty, "Invalid property: #{key}" unless valid_property?(key)

    write_attribute(key, value)
  end
end

#update_doc(key, _value = nil) ⇒ Object



35
36
37
# File 'lib/dolly/property_manager.rb', line 35

def update_doc(key, _value = nil)
  doc[key.to_s] = instance_variable_get(:"@#{key}")
end

#valid_property?(name) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/dolly/property_manager.rb', line 31

def valid_property?(name)
  properties.include?(name.to_sym)
end

#write_attribute(key, value) ⇒ Object



25
26
27
28
29
# File 'lib/dolly/property_manager.rb', line 25

def write_attribute(key, value)
  casted_value = set_property_value(key, value)
  instance_variable_set(:"@#{key}", casted_value)
  update_doc(key)
end