lib/rasti/form.rb in rasti-form-1.0.3 vs lib/rasti/form.rb in rasti-form-1.1.0
- old
+ new
@@ -54,16 +54,20 @@
def to_s
"#<#{self.class.name || self.class.superclass.name}[#{to_h.map { |n,v| "#{n}: #{v.inspect}" }.join(', ')}]>"
end
alias_method :inspect, :to_s
- def attributes
- assigned_attribute_names.each_with_object({}) do |name, hash|
+ def attributes(options={})
+ attributes_filter = {only: assigned_attribute_names, except: []}.merge(options)
+ (attributes_filter[:only] - attributes_filter[:except]).each_with_object({}) do |name, hash|
hash[name] = serialize(read_attribute(name))
end
end
- alias_method :to_h, :attributes
+
+ def to_h
+ attributes
+ end
def assigned?(name)
assigned_attribute_names.include? name
end
@@ -110,10 +114,10 @@
end
end
end
def assigned_attribute_names
- self.class.attribute_names & instance_variables.map { |v| v.to_s[1..-1].to_sym }
+ @assigned_attribute_names ||= self.class.attribute_names & instance_variables.map { |v| v.to_s[1..-1].to_sym }
end
def serialize(value)
if value.kind_of? Array
value.map { |v| serialize v }
\ No newline at end of file