class Yukata::Base
Public Class Methods
attr_accessor(*args)
click to toggle source
@override
Calls superclass method
# File lib/yukata/base.rb, line 39 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/yukata/base.rb, line 49 def self.attribute(name, type=String, options={}) config = { writer: true, reader: true, coerce: true }.merge(options) attr = Attribute.new(type, options) self.attributes[name] = attr variable = "@#{name}" # ------------- # Define Writer # ------------- if config[:writer] define_method("#{name}=") do |object| if config[:coerce] val = Yukata.coercer.coerce(object, attr.type) instance_variable_set(variable, val) else instance_variable_set(variable, object) end end end # ------------- # Define reader # ------------- 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/yukata/base.rb, line 34 def self.attributes @attributes ||= {} end
new(params={})
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/yukata/base.rb, line 6 def initialize(params={}) self.attributes = params end
Public Instance Methods
attributes()
click to toggle source
Get the attributes on the model. If the attribute was not
@return [Hash]
# File lib/yukata/base.rb, line 22 def attributes hash = {} self.class.attributes.keys.each do |k| hash[k] = send(k) if respond_to?(k) end hash end
attributes=(hash)
click to toggle source
Sets the attributes on the model @param hash [Hash]
# File lib/yukata/base.rb, line 12 def attributes=(hash) hash.each do |k, v| setter = "#{k}=" self.send(setter, v) if self.respond_to?(setter) end end
to_h()
click to toggle source
# File lib/yukata/base.rb, line 30 def to_h self.attributes end