lib/shamu/attributes.rb in shamu-0.0.13 vs lib/shamu/attributes.rb in shamu-0.0.14
- old
+ new
@@ -25,11 +25,13 @@
extend ActiveSupport::Concern
require "shamu/attributes/assignment"
require "shamu/attributes/fluid_assignment"
require "shamu/attributes/validation"
+ require "shamu/attributes/validators"
require "shamu/attributes/equality"
+ require "shamu/attributes/camel_case"
require "shamu/attributes/html_sanitation"
def initialize( *attributes )
assign_attributes( attributes.last )
end
@@ -106,23 +108,22 @@
end
end
def build_value( build, value )
if build.is_a?( Class )
- klass = build
- build = ->(v) { klass.new( v ) }
+ build.new( value )
+ else
+ build.call( value )
end
-
- build.call( value )
end
def resolve_attributes( attributes )
if attributes.respond_to?( :to_attributes )
attributes.to_attributes
# Allow protected attributes to be used without explicitly being set.
- # All 'Attributes' classes are them selves the explicit set of permitted
- # attributes.
+ # All 'Attributes' classes are themselves the explicit set of permitted
+ # attributes so there is no danger of 'over assignment'.
elsif attributes.respond_to?( :to_unsafe_h )
attributes.to_unsafe_h
elsif attributes.respond_to?( :to_hash )
attributes.to_hash.symbolize_keys
elsif attributes.respond_to?( :to_h )
@@ -159,11 +160,11 @@
# @param [Symbol] as an alias of the attribute.
# @param [Symbol] on another method on the class to delegate the attribute
# to.
# @param [Object,#call] default value if not set.
# @param [Class,#call] build method used to build a nested object on
- # assignement of a hash with nested keys.
+ # assignment of a hash with nested keys.
# @param [Boolean] serialize true if the attribute should be included in
# {#to_attributes}. Default true.
# @yieldreturn the value of the attribute. The result is memoized so the
# block is only invoked once.
# @return [self]
@@ -201,11 +202,11 @@
private
# @return [Array<Symbol>] keys used by the {.attribute} method options
# argument. Used by {Attributes::Validation} to filter option keys.
def attribute_option_keys
- [ :on, :build, :default, :serialize ]
+ [ :on, :build, :default, :serialize, :as ]
end
def create_attribute( name, *args, **options )
options = options.dup
options[:build] = args[0] unless args.blank?
@@ -216,9 +217,13 @@
def define_attribute_reader( name, as: nil, ** )
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{ name } # def attribute
return @#{ name } if defined? @#{ name } # return @attribute if defined? @attribute
@#{ name } = fetch_#{ name } # @attribute = fetch_attribute
+ end # end
+
+ def #{ name }_set? # def attribute_set?
+ defined? @#{ name } # defined? @attribute
end # end
RUBY
alias_method as, name if as
end