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
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
- .ignore_in_json(*attributes) ⇒ 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
- #to_h ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Entity
Returns a new instance of Entity.
60 61 62 63 64 65 |
# File 'lib/duse/client/entity.rb', line 60 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)
Returns the value of attribute attributes.
58 59 60 |
# File 'lib/duse/client/entity.rb', line 58 def attributes @attributes end |
#curry ⇒ Object
Returns the value of attribute curry.
57 58 59 |
# File 'lib/duse/client/entity.rb', line 57 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
52 53 54 55 |
# File 'lib/duse/client/entity.rb', line 52 def self.id_field(key = nil) @id_field = key.to_s if key @id_field end |
.ignore_in_json(*attributes) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/duse/client/entity.rb', line 44 def self.ignore_in_json(*attributes) @ignored_attributes ||= [] attributes.each do |attribute| @ignored_attributes << attribute end @ignored_attributes 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
84 85 86 |
# File 'lib/duse/client/entity.rb', line 84 def delete curry.delete id end |
#load_attribute(name) ⇒ Object
71 72 73 74 |
# File 'lib/duse/client/entity.rb', line 71 def load_attribute(name) reload if missing? name attributes[name.to_s] end |
#missing?(name) ⇒ Boolean
88 89 90 91 |
# File 'lib/duse/client/entity.rb', line 88 def missing?(name) return false unless self.class.attributes.include? name !attributes.key?(name) end |
#reload ⇒ Object
76 77 78 |
# File 'lib/duse/client/entity.rb', line 76 def reload attributes.merge! curry.find_one(id).attributes end |
#save ⇒ Object
80 81 82 |
# File 'lib/duse/client/entity.rb', line 80 def save fail NotImplementedError, 'Save will be the "update" action, once the api supports it' end |
#set_attribute(name, value) ⇒ Object
67 68 69 |
# File 'lib/duse/client/entity.rb', line 67 def set_attribute(name, value) attributes[name.to_s] = value end |
#to_h ⇒ Object
93 94 95 96 97 |
# File 'lib/duse/client/entity.rb', line 93 def to_h result = attributes.clone self.class.ignore_in_json.each { |e| result.delete e.to_s } result end |