Module: Valuable::Utils
- Defined in:
- lib/valuable/utils.rb
Class Method Summary collapse
- .can_be_duplicated?(item) ⇒ Boolean
-
.check_options_validity(class_name, attribute, options) ⇒ Object
this helper raises an exception if the options passed to has_value are wrong.
- .deep_duplicate_of(value) ⇒ Object
- .find_attribute_for(name, attributes) ⇒ Object
- .format(name, value, attributes, collection_item = false) ⇒ Object
- .formatters ⇒ Object
- .initial_copy_of_attributes(atts) ⇒ Object
- .klass_options ⇒ Object
- .known_options ⇒ Object
Class Method Details
.can_be_duplicated?(item) ⇒ Boolean
121 122 123 124 125 126 |
# File 'lib/valuable/utils.rb', line 121 def can_be_duplicated?( item ) Marshal.dump(item) true rescue false end |
.check_options_validity(class_name, attribute, options) ⇒ Object
this helper raises an exception if the options passed to has_value are wrong. Mostly written because I occasionally used :class instead of :klass and, being a moron, wasted time trying to find the issue.
131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/valuable/utils.rb', line 131 def ( class_name, attribute, ) = .keys - raise ArgumentError, "#{class_name}##{attribute} has a default value that must be set using a lambda. Use :default => lambda { Thing.new }." if [:default] && ![:default].kind_of?(Proc) && !can_be_duplicated?( [:default] ) raise ArgumentError, "has_value did not know how to respond to option(s) #{.join(', ')}. Valid (optional) arguments are: #{.join(', ')}" unless .empty? raise ArgumentError, "#{class_name} doesn't know how to format #{attribute} with :klass => #{[:klass].inspect}" unless .any?{|klass| klass === [:klass]} raise( ArgumentError, "#{class_name} can't promise to return a(n) #{[:klass]} when using :parse_with" ) if [:klass].is_a?( Symbol ) && [:parse_with] end |
.deep_duplicate_of(value) ⇒ Object
33 34 35 |
# File 'lib/valuable/utils.rb', line 33 def deep_duplicate_of(value) Marshal.load(Marshal.dump(value)) end |
.find_attribute_for(name, attributes) ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/valuable/utils.rb', line 9 def find_attribute_for( name, attributes ) name = name.to_sym if attributes.keys.include?( name ) name elsif found=attributes.find{|n, v| v[:alias].to_sym == name } found[0] end end |
.format(name, value, attributes, collection_item = false) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/valuable/utils.rb', line 37 def format( name, value, attributes, collection_item = false ) klass = collection_item ? attributes[name][:item_klass] : attributes[name][:klass] case klass when *formatters.keys formatters[klass].call(value) when NilClass if Proc === attributes[name][:parse_with] attributes[name][:parse_with].call(value) else value end when :collection value.map do |item| Valuable::Utils.format( name, item, attributes, true ) end when :date case value.class.to_s when "Date" value when "ActiveSupport::TimeWithZone", "Time", "DateTime" value.to_date when "String" value && begin; Date.parse(value); rescue; end else value end when :integer value.to_i if value && value != '' when :decimal case value when NilClass nil when BigDecimal value else BigDecimal.new( value.to_s ) end when :string value && value.to_s when :boolean value == '0' ? false : !!value else if value.nil? nil elsif value.is_a? klass value elsif Proc === attributes[name][:parse_with] attributes[name][:parse_with].call(value) else klass.send( attributes[name][:parse_with] || :new, value) end end unless value.nil? end |
.formatters ⇒ Object
109 110 111 |
# File 'lib/valuable/utils.rb', line 109 def formatters @formatters ||= {} end |
.initial_copy_of_attributes(atts) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/valuable/utils.rb', line 19 def initial_copy_of_attributes(atts) out = {} atts.each do |name, value| case value when Proc out[name] = value.call else out[name] = deep_duplicate_of( value ) end end out end |
.klass_options ⇒ Object
113 114 115 |
# File 'lib/valuable/utils.rb', line 113 def [NilClass, :integer, Class, :date, :decimal, :string, :boolean] + formatters.keys end |
.known_options ⇒ Object
117 118 119 |
# File 'lib/valuable/utils.rb', line 117 def [:klass, :default, :negative, :alias, :parse_with, :extend, :allow_blank] end |