Class: Duse::Client::Entity
- Inherits:
-
Object
- Object
- Duse::Client::Entity
- Defined in:
- lib/duse/client/entity.rb
Constant Summary collapse
- MAP =
{}
Instance Attribute Summary collapse
-
#attributes ⇒ Object
(also: #to_h)
readonly
Returns the value of attribute attributes.
-
#curry ⇒ Object
Returns the value of attribute curry.
Class Method Summary collapse
- .add_attribute(name) ⇒ Object
- .attributes(*list) ⇒ Object
- .base_path ⇒ Object
- .id_field(key = nil) ⇒ Object
- .many(key = nil) ⇒ Object
- .one(key = nil) ⇒ Object
- .subclasses ⇒ Object
Instance Method Summary collapse
- #delete ⇒ Object
-
#initialize(options = {}) ⇒ Entity
constructor
A new instance of Entity.
- #load_attribute(name) ⇒ Object
- #missing?(name) ⇒ Boolean
- #reload ⇒ Object
- #save ⇒ Object
- #set_attribute(name, value) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Entity
Returns a new instance of Entity.
53 54 55 56 57 58 |
# File 'lib/duse/client/entity.rb', line 53 def initialize( = {}) @attributes = {} .each do |key, value| self.send("#{key.to_s}=", value) if respond_to? "#{key.to_s}=" end end |
Instance Attribute Details
#attributes ⇒ Object (readonly) Also known as: to_h
Returns the value of attribute attributes.
50 51 52 |
# File 'lib/duse/client/entity.rb', line 50 def attributes @attributes end |
#curry ⇒ Object
Returns the value of attribute curry.
49 50 51 |
# File 'lib/duse/client/entity.rb', line 49 def curry @curry end |
Class Method Details
.add_attribute(name) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/duse/client/entity.rb', line 35 def self.add_attribute(name) dummy = self.new attributes << name define_method(name) { load_attribute(name) } unless dummy.respond_to? name define_method("#{name}=") { |value| set_attribute(name, value) } unless dummy.respond_to? "#{name}=" define_method("#{name}?") { !!send(name) } unless dummy.respond_to? "#{name}?" end |
.attributes(*list) ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/duse/client/entity.rb', line 24 def self.attributes(*list) @attributes ||= [] list.each do |name| add_attribute name.to_s end @attributes end |
.base_path ⇒ Object
6 7 8 |
# File 'lib/duse/client/entity.rb', line 6 def self.base_path many end |
.id_field(key = nil) ⇒ Object
44 45 46 47 |
# File 'lib/duse/client/entity.rb', line 44 def self.id_field(key = nil) @id_field = key.to_s if key @id_field end |
.many(key = nil) ⇒ Object
19 20 21 22 |
# File 'lib/duse/client/entity.rb', line 19 def self.many(key = nil) MAP[key.to_s] = self if key @many ||= key.to_s end |
.one(key = nil) ⇒ Object
14 15 16 17 |
# File 'lib/duse/client/entity.rb', line 14 def self.one(key = nil) MAP[key.to_s] = self if key @one ||= key.to_s end |
.subclasses ⇒ Object
10 11 12 |
# File 'lib/duse/client/entity.rb', line 10 def self.subclasses MAP.values.uniq end |
Instance Method Details
#delete ⇒ Object
77 78 79 |
# File 'lib/duse/client/entity.rb', line 77 def delete curry.delete id end |
#load_attribute(name) ⇒ Object
64 65 66 67 |
# File 'lib/duse/client/entity.rb', line 64 def load_attribute(name) reload if missing? name attributes[name.to_s] end |
#missing?(name) ⇒ Boolean
81 82 83 84 |
# File 'lib/duse/client/entity.rb', line 81 def missing?(name) return false unless self.class.attributes.include? name !attributes.key?(name) end |
#reload ⇒ Object
69 70 71 |
# File 'lib/duse/client/entity.rb', line 69 def reload attributes.merge! curry.find_one(id).attributes end |
#save ⇒ Object
73 74 75 |
# File 'lib/duse/client/entity.rb', line 73 def save fail NotImplementedError, 'Save will be the "update" action, once the api supports it' end |
#set_attribute(name, value) ⇒ Object
60 61 62 |
# File 'lib/duse/client/entity.rb', line 60 def set_attribute(name, value) attributes[name.to_s] = value end |