module FastAttributes
Constants
- FALSE_VALUES
- SINGLETON_CLASSES
- TRUE_VALUES
- VERSION
Public Class Methods
add_default_attribute(klass, attribute, value)
click to toggle source
# File lib/fast_attributes/default_attributes.rb, line 18 def add_default_attribute(klass, attribute, value) @default_attributes ||= {} @default_attributes[klass] ||= {} @default_attributes[klass][attribute] = value end
cloneable?(value)
click to toggle source
# File lib/fast_attributes/default_attributes.rb, line 24 def cloneable?(value) case value when *SINGLETON_CLASSES false else true end end
default_attributes(klass)
click to toggle source
# File lib/fast_attributes/default_attributes.rb, line 5 def default_attributes(klass) return {} unless (@default_attributes || {})[klass] @default_attributes[klass].each_with_object({}) do |(attribute, value), memo| memo[attribute] = if value.respond_to?(:call) value.call elsif cloneable?(value) value.clone else value end end end
get_type_casting(klass)
click to toggle source
# File lib/fast_attributes.rb, line 18 def get_type_casting(klass) type_casting[klass] end
remove_type_casting(klass)
click to toggle source
# File lib/fast_attributes.rb, line 31 def remove_type_casting(klass) type_casting.delete(klass) end
set_type_casting(klass, casting)
click to toggle source
# File lib/fast_attributes.rb, line 22 def set_type_casting(klass, casting) symbol = klass.name.gsub(/([a-z])([A-Z])/, '\1_\2').downcase.to_sym # DateTime => :date_time type_cast symbol, klass do from 'nil', to: 'nil' from klass.name, to: '%s' otherwise casting end end
type_cast(*types_or_classes, &block)
click to toggle source
# File lib/fast_attributes.rb, line 39 def type_cast(*types_or_classes, &block) types_or_classes.each do |type_or_class| type_cast = TypeCast.new(type_or_class) type_cast.instance_eval(&block) type_casting[type_or_class] = type_cast end end
type_casting()
click to toggle source
# File lib/fast_attributes.rb, line 14 def type_casting @type_casting ||= {} end
type_exists?(klass)
click to toggle source
# File lib/fast_attributes.rb, line 35 def type_exists?(klass) type_casting.has_key?(klass) end
Public Instance Methods
attribute(*attributes, type)
click to toggle source
# File lib/fast_attributes.rb, line 54 def attribute(*attributes, type) builder = Builder.new(self) builder.attribute *attributes, type builder.compile! end
define_attributes(options = {}, &block)
click to toggle source
# File lib/fast_attributes.rb, line 48 def define_attributes(options = {}, &block) builder = Builder.new(self, options) builder.instance_eval(&block) builder.compile! end