Module: Relaxo::Model::Component
- Defined in:
- lib/relaxo/model/component.rb,
lib/relaxo/model/attachments.rb
Constant Summary collapse
- ATTACHMENTS =
'_attachments'
- DEFAULT_ATTACHMENT_CONTENT_TYPE =
'application/octet-stream'
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#changed ⇒ Object
readonly
Returns the value of attribute changed.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Class Method Summary collapse
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #assign(primative_attributes, only = :all) ⇒ Object
-
#attach(path, data, options = {}) ⇒ Object
Attach a file to the document with a given path.
-
#attachments(prefix = nil) ⇒ Object
Get all attachments, optionally filtering with a particular prefix path.
- #clear(key) ⇒ Object
- #flatten! ⇒ Object
- #initialize(database, attributes = {}) ⇒ Object
- #to_hash ⇒ Object
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
48 49 50 |
# File 'lib/relaxo/model/component.rb', line 48 def attributes @attributes end |
#changed ⇒ Object (readonly)
Returns the value of attribute changed.
50 51 52 |
# File 'lib/relaxo/model/component.rb', line 50 def changed @changed end |
#database ⇒ Object (readonly)
Returns the value of attribute database.
49 50 51 |
# File 'lib/relaxo/model/component.rb', line 49 def database @database end |
Class Method Details
.included(child) ⇒ Object
36 37 38 39 |
# File 'lib/relaxo/model/component.rb', line 36 def self.included(child) # $stderr.puts "#{self} included -> #{child} extend Base" child.send(:extend, Base) end |
Instance Method Details
#[](name) ⇒ Object
80 81 82 83 84 85 86 87 88 |
# File 'lib/relaxo/model/component.rb', line 80 def [] name name = name.to_s if self.class.properties.include? name self.send(name) else raise KeyError.new(name) end end |
#[]=(name, value) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/relaxo/model/component.rb', line 90 def []= name, value name = name.to_s if self.class.properties.include? name self.send("#{name}=", value) else raise KeyError.new(name) end end |
#assign(primative_attributes, only = :all) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/relaxo/model/component.rb', line 57 def assign(primative_attributes, only = :all) enumerator = primative_attributes if only == :all enumerator = enumerator.select{|key, value| self.class.properties.include? key.to_s} elsif only.respond_to? :include? enumerator = enumerator.select{|key, value| only.include? key.to_sym} end enumerator.each do |key, value| key = key.to_s klass = self.class.properties[key] if klass # This might raise a validation error value = klass.convert_from_primative(@database, value) end self[key] = value end end |
#attach(path, data, options = {}) ⇒ Object
Attach a file to the document with a given path.
31 32 33 34 35 |
# File 'lib/relaxo/model/attachments.rb', line 31 def attach(path, data, = {}) [:content_type] ||= DEFAULT_ATTACHMENT_CONTENT_TYPE @database.attach(@attributes, path, data, ) end |
#attachments(prefix = nil) ⇒ Object
Get all attachments, optionally filtering with a particular prefix path.
38 39 40 41 42 43 44 45 46 |
# File 'lib/relaxo/model/attachments.rb', line 38 def (prefix = nil) = (@attributes[ATTACHMENTS] || []) if prefix .select{|name, | name.start_with? prefix} else end end |
#clear(key) ⇒ Object
52 53 54 55 |
# File 'lib/relaxo/model/component.rb', line 52 def clear(key) @changed.delete(key) @attributes.delete(key) end |
#flatten! ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/relaxo/model/component.rb', line 100 def flatten! errors = [] # Flatten changed properties: self.class.properties.each do |key, klass| if @changed.include? key if klass begin @attributes[key] = klass.convert_to_primative(@changed.delete(key)) rescue StandardError => error errors << Error.new(key, error) end else @attributes[key] = @changed.delete(key) end end end # Non-specific properties, serialised by JSON: @changed.each do |key, value| @attributes[key] = value end @changed = {} errors end |
#initialize(database, attributes = {}) ⇒ Object
41 42 43 44 45 46 |
# File 'lib/relaxo/model/component.rb', line 41 def initialize(database, attributes = {}) # Raw key-value database @attributes = attributes @database = database @changed = {} end |
#to_hash ⇒ Object
128 129 130 |
# File 'lib/relaxo/model/component.rb', line 128 def to_hash @attributes end |