lib/active_house/modeling.rb in active_house-0.1.2 vs lib/active_house/modeling.rb in active_house-0.1.3
- old
+ new
@@ -16,11 +16,11 @@
is_setter = !is_setter.nil?
[name, is_setter]
end
def attribute_method?(name, is_setter, *args)
- (_attribute_opts.key?(name) || @_attributes.key?(name)) && (is_setter ? args.size.one? : true)
+ (_attribute_opts.key?(name) || @_attributes.key?(name)) && (is_setter ? args.size == 1 : true)
end
def get_attribute(name)
@_attributes[name]
end
@@ -38,15 +38,21 @@
def attributes(*names)
options = names.extract_options!
names.each { |name| attributes(name, options.dup) }
end
+
+ def load!(params)
+ new.tap do |model|
+ params.each { |name, value| model[name] = value }
+ end
+ end
end
def initialize(params = {})
@_attributes = {}
- assign_attributes(params)
+ assign_attributes(params) unless params.nil?
end
def as_json(*_args)
to_h
end
@@ -55,9 +61,19 @@
@_attributes.dup
end
def [](key)
@_attributes[key.to_sym]
+ end
+
+ def []=(key, value)
+ @_attributes[key.to_sym] = value
+ end
+
+ def assign_attributes(params)
+ params.each do |key, val|
+ public_send("#{key}=", val)
+ end
end
def respond_to_missing?(method_name, *args)
name, is_setter = parse_attribute_method_name(method_name)
attribute_method?(name, is_setter, *args)