Module: Dolly::Properties

Included in:
Document, DocumentCreation
Defined in:
lib/dolly/properties.rb

Constant Summary collapse

SPECIAL_KEYS =
%i[_id _rev]

Instance Method Summary collapse

Instance Method Details

#all_property_keysObject



40
41
42
# File 'lib/dolly/properties.rb', line 40

def all_property_keys
  properties.map(&:key) + SPECIAL_KEYS
end

#propertiesObject



36
37
38
# File 'lib/dolly/properties.rb', line 36

def properties
  @properties ||= PropertySet.new
end

#property(*opts, class_name: nil, default: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/dolly/properties.rb', line 8

def property *opts, class_name: nil, default: nil
  opts.each do |opt|
    properties << (prop = Property.new(opt, class_name, default))

    silence_redefinition_of_method(opt.to_sym)
    silence_redefinition_of_method(:"#{opt}=")
    silence_redefinition_of_method(:"#{opt}?") if prop.boolean?
    silence_redefinition_of_method(:"[]")

    send(:attr_reader, opt)

    define_method(:"#{opt}=") do |value|
      write_attribute(opt, value)
    end

    define_method(:"#{opt}?") { send(opt) } if prop.boolean?
    define_method(:"[]") { |name| send(name) }
  end
end

#property_clean_doc(doc) ⇒ Object



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

def property_clean_doc(doc)
  doc.select { |key, _value| property_keys.include?(key.to_sym) }
end

#property_keysObject



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

def property_keys
  all_property_keys - SPECIAL_KEYS
end

#silence_redefinition_of_method(method) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/dolly/properties.rb', line 28

def silence_redefinition_of_method(method)
  if method_defined?(method) || private_method_defined?(method)
    # This suppresses the "method redefined" warning; the self-alias
    # looks odd, but means we don't need to generate a unique name
    alias_method method, method
  end
end