class RandomAttributes::Register
Attributes
aliased[R]
originals[R]
parent_node[R]
try_node[R]
type[R]
Public Class Methods
new(attrs, options = {})
click to toggle source
# File lib/random_attributes.rb, line 28 def initialize(attrs, options = {}) if attrs.is_a?(Array) raise AliasError, "if you want to alias attributes you need to specify the alias in the options" unless options[:alias].present? @aliased = options[:alias].to_s else @aliased = options[:alias].to_s.presence || attrs.underscore attrs = [attrs] end @originals = attrs @parent_node = options[:within] @try_node = options[:try] @type = options[:type] @model = options[:model] @collection = options[:collection] @cast_proc = options[:parse] || cast_to_proc end
Public Instance Methods
cast(value)
click to toggle source
# File lib/random_attributes.rb, line 53 def cast(value) @cast_proc.call value end
key()
click to toggle source
# File lib/random_attributes.rb, line 49 def key @originals.join '__' end
Private Instance Methods
cast_to_proc()
click to toggle source
TODO refactor this.
# File lib/random_attributes.rb, line 60 def cast_to_proc if @type ->(value) { value ? Kernel.send(@type.to_s, value) : value } elsif @model ->(value) { value ? @model.parse(value) : value } elsif @collection if @collection.respond_to?(:parse) ->(value) { if value value.map do |member| member.is_a?(@collection) ? member : @collection.parse(member) end else [] end } else ->(value) { if value value.map do |member| member.is_a?(@collection) ? member : @collection.new(member) end else [] end } end else ->(value) { value } end end