class Wardrobe::Attribute
Attribute
class
Attributes
getters[R]
ivar_name[R]
klass[R]
name[R]
options[R]
setter_name[R]
setters[R]
store[R]
Public Class Methods
new(name, klass, defining_object, config, **options)
click to toggle source
# File lib/wardrobe/attribute.rb, line 11 def initialize(name, klass, defining_object, config, **options) @name = name @ivar_name = "@#{name}" @setter_name = "#{name}=" @klass = validate_klass(klass) @options = validate_options(options, config, defining_object) @getters ||= build_getter_array(defining_object) @setters ||= build_setter_array(defining_object) freeze end
Public Instance Methods
coerce(val)
click to toggle source
# File lib/wardrobe/plugins/coercible.rb, line 20 def coerce(val) klass.coerce(val, self) rescue Plugins::Coercible::Refinements::UnsupportedError => e raise e.class, "Can't coerce #{val.class} `#{val}` into #{klass}" end
merge(other, defining_object, config)
click to toggle source
# File lib/wardrobe/attribute.rb, line 34 def merge(other, defining_object, config) merged_options = merge_options(other.options) self.class.new(name, other.klass, defining_object, config, merged_options) end
validate_klass(klass)
click to toggle source
# File lib/wardrobe/attribute.rb, line 22 def validate_klass(klass) if klass.is_a?(Array) && klass.count != 1 raise StandardError, %( `Array#{klass.map(&:name)}' contains two many classes. No more than one is allowed. ) elsif klass.is_a?(Hash) # TODO: Validate hash end klass end
Private Instance Methods
build_getter_array(klass)
click to toggle source
# File lib/wardrobe/attribute.rb, line 55 def build_getter_array(klass) (klass.option_store.values.map do |option| option.getter if option.use_getter_for_atr?(self) end + klass.default_getters).compact.sort end
build_setter_array(klass)
click to toggle source
# File lib/wardrobe/attribute.rb, line 61 def build_setter_array(klass) (klass.option_store.values.map do |option| option.setter if option.use_setter_for_atr?(self) end + klass.default_setters).compact.sort end
merge_options(other)
click to toggle source
# File lib/wardrobe/attribute.rb, line 41 def merge_options(other) merged_options = options.dup other.each do |key, value| next if merged_options[key] == value if merged_options[key] raise 'FIX ME! Currently only hash merge is supported' unless value.is_a?(Hash) merged_options[key] = merged_options[key].merge(value) else merged_options[key] = value.dup end end merged_options end
validate_options(options, config, defining_object)
click to toggle source
# File lib/wardrobe/attribute.rb, line 67 def validate_options(options, config, defining_object) options.each do |name, _| unless config.option_store[name] Wardrobe.logger.error "Option '#{name}' is unavailable for attribute '#{self.name}' on '#{defining_object}'" raise UnavailableOptionError end end end