Module: Bodhi::Properties::InstanceMethods
- Defined in:
- lib/bodhi-slam/properties.rb
Instance Method Summary collapse
-
#attributes ⇒ Object
Returns a Hash of the class properties and their values.
- #each ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Object
Initializes a new instance of the class.
- #new_record? ⇒ Boolean
- #persisted? ⇒ Boolean
-
#to_json(base = nil) ⇒ Object
Returns all the classes properties as JSON.
-
#update_attributes(params) ⇒ Object
Updates the resource with the given attributes Hash.
Instance Method Details
#attributes ⇒ Object
Returns a Hash of the class properties and their values.
object = SomeResource.new(foo:"test", bar:12345)
object.attributes # => { foo: "test", bar: 12345 }
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/bodhi-slam/properties.rb', line 62 def attributes attributes = Hash.new self.class.property_names.each do |property| value = send(property) if value.respond_to?(:attributes) attributes[property] = value.attributes.delete_if { |k, v| v.nil? } elsif value.is_a?(Array) && value.first.respond_to?(:attributes) attributes[property] = value.map(&:attributes).collect{ |item| item.delete_if { |k, v| v.nil? } } elsif value.is_a?(Time) attributes[property] = value.iso8601 else attributes[property] = value end end attributes.delete_if { |k, v| v.nil? } attributes end |
#each ⇒ Object
23 |
# File 'lib/bodhi-slam/properties.rb', line 23 def each; attributes.each{ |k, v| yield(k, v) }; end |
#id ⇒ Object
20 |
# File 'lib/bodhi-slam/properties.rb', line 20 def id; @sys_id; end |
#initialize(options = {}) ⇒ Object
Initializes a new instance of the class. Accepts a parameter Hash for mass assignment. If a parameter does not exist for the class, an UndefinedMethod error is raised.
klass = Class.new do
include Bodhi::Properties
property :name, :email
end
object = klass.new(name: "Bob", email: "[email protected]")
object.name #=> "Bob"
object.email #=> "[email protected]"
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bodhi-slam/properties.rb', line 36 def initialize(={}) if .is_a?(String) = JSON.parse() end # Set properties defined by the +options+ parameter = Bodhi::Support.symbolize_keys() .each do |property, value| = self.class.properties[property] if .nil? send("#{property}=", value) else send("#{property}=", Bodhi::Support.coerce(value, )) end end # Set any default values self.class.properties.select{ |k,v| !v[:default].nil? }.each do |property, | send("#{property}=", [:default]) if send("#{property}").nil? end end |
#new_record? ⇒ Boolean
22 |
# File 'lib/bodhi-slam/properties.rb', line 22 def new_record?; @sys_id.nil?; end |
#persisted? ⇒ Boolean
21 |
# File 'lib/bodhi-slam/properties.rb', line 21 def persisted?; !@sys_id.nil?; end |
#to_json(base = nil) ⇒ Object
Returns all the classes properties as JSON. It converts any nested objects to JSON if they respond to to_json
resource = SomeResource.new(foo:"test", bar:12345)
= AnotherResource.new( test: resource )
resource.to_json # => "{ 'foo':'test', 'bar':12345 }"
.to_json # => "{ 'test': { 'foo':'test', 'bar':12345 } }"
107 108 109 |
# File 'lib/bodhi-slam/properties.rb', line 107 def to_json(base=nil) attributes.to_json end |
#update_attributes(params) ⇒ Object
Updates the resource with the given attributes Hash
s = SomeResource.factory.build(foo:"test", bar:12345)
s.attributes # => { foo: "test", bar: 12345 }
s.update_attributes(bar: 10)
s.attributes # => { foo: "test", bar: 10 }
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/bodhi-slam/properties.rb', line 88 def update_attributes(params) params.each do |property, value| = self.class.properties[property.to_sym] if .nil? send("#{property}=", value) else send("#{property}=", Bodhi::Support.coerce(value, )) end end end |