class Croesus::Attributes
Public Class Methods
attr_accessor(*args)
click to toggle source
@override
Calls superclass method
# File lib/croesus/attributes.rb, line 57 def self.attr_accessor(*args) args.each { |name| self.attributes[name] = Attribute.new(Object) } super end
attribute(name, type=String, options={})
click to toggle source
Declares an attribute on the model
@param name [String] @param type [Class] a class that represents the type @param options [Hash] extra options to apply to the attribute
# File lib/croesus/attributes.rb, line 67 def self.attribute(name, type=String, options={}) config = { writer: true, reader: true }.merge(options) attr = Attribute.new(type, options) self.attributes[name] = attr variable = "@#{name}" if config[:writer] define_method("#{name}=") do |object| val = Croesus.coercer.coerce(object, attr.type) instance_variable_set(variable, val) end end if config[:reader] define_method(name) do val = instance_variable_get(variable) unless val val = attr.default instance_variable_set(variable, val) end val end end end
attributes()
click to toggle source
# File lib/croesus/attributes.rb, line 52 def self.attributes @attributes ||= {} end
new(attributes = {})
click to toggle source
@param params [Hash] the parameters you wish to initialize the model
with. If the model does not have an accessor set, it will ignore the attribute passed.
# File lib/croesus/attributes.rb, line 25 def initialize(attributes = {}) self.attributes = attributes end
Public Instance Methods
attributes()
click to toggle source
Get and ser the attributes on the model.
@return [Hash]
# File lib/croesus/attributes.rb, line 40 def attributes hash = {} self.class.attributes.keys.each do |k| hash[k] = send(k) if respond_to?(k) end hash end
attributes=(attributes = {})
click to toggle source
Sets the attributes on the model @param attributes [Hash]
# File lib/croesus/attributes.rb, line 31 def attributes=(attributes = {}) attributes.each do |attr, value| self.send("#{attr}=", value) if self.respond_to?("#{attr}=") end end
to_h()
click to toggle source
# File lib/croesus/attributes.rb, line 48 def to_h self.attributes end