Module: DataBindings::Bound
- Included in:
- BoundArray, BoundObject
- Defined in:
- lib/data_bindings/bound.rb
Defined Under Namespace
Classes: BoundArray, BoundObject, Errors
Constant Summary collapse
- ValidationError =
Class.new(RuntimeError)
- NoBindingName =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
include DataBindings::WriterInterceptor.
-
#generator ⇒ Object
readonly
include DataBindings::WriterInterceptor.
-
#name ⇒ Object
readonly
include DataBindings::WriterInterceptor.
-
#source ⇒ Object
readonly
include DataBindings::WriterInterceptor.
Instance Method Summary collapse
- #calculate_validness ⇒ Object
- #cast_element(lookup_name, source, type, opts = nil, &blk) ⇒ Object
- #pre_convert ⇒ Object
- #valid! ⇒ Object
- #valid? ⇒ Boolean
Instance Attribute Details
#errors ⇒ Object (readonly)
include DataBindings::WriterInterceptor
25 26 27 |
# File 'lib/data_bindings/bound.rb', line 25 def errors @errors end |
#generator ⇒ Object (readonly)
include DataBindings::WriterInterceptor
25 26 27 |
# File 'lib/data_bindings/bound.rb', line 25 def generator @generator end |
#name ⇒ Object (readonly)
include DataBindings::WriterInterceptor
25 26 27 |
# File 'lib/data_bindings/bound.rb', line 25 def name @name end |
#source ⇒ Object (readonly)
include DataBindings::WriterInterceptor
25 26 27 |
# File 'lib/data_bindings/bound.rb', line 25 def source @source end |
Instance Method Details
#calculate_validness ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/data_bindings/bound.rb', line 32 def calculate_validness if @last_hash.nil? || @last_hash != hash @from = self if @last_hash errors.clear validate @last_hash = hash end end |
#cast_element(lookup_name, source, type, opts = nil, &blk) ⇒ Object
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/data_bindings/bound.rb', line 50 def cast_element(lookup_name, source, type, opts = nil, &blk) name = get_parameter_name(lookup_name, source, opts) el = name && source[name] raise_on_error = opts && opts.key?(:raise_on_error) ? opts[:raise_on_error] : false allow_nil = opts && opts.key?(:allow_nil) ? opts[:allow_nil] : false el ||= opts[:default] if opts && opts.key?(:default) if el.nil? && name.nil? @errors[lookup_name] = generate_error("not found", raise_on_error) nil else new_el = if type.nil? # anything goes case el when Array, Hash blk ? register_sub(name, @generator.from_ruby(el).bind(&blk), raise_on_error) : el else el end elsif type == String @errors[lookup_name] = generate_error("was not a String", raise_on_error) unless el.is_a?(String) el elsif type == Integer begin Integer(el) rescue ArgumentError, TypeError @errors[lookup_name] = generate_error("was not an Integer", raise_on_error) el end elsif type == Float begin Float(el) rescue ArgumentError, TypeError @errors[lookup_name] = generate_error("was not a Float", raise_on_error) el end elsif Array === type if el.is_a?(Array) @errors[lookup_name] = generate_error("did not match the length", raise_on_error) if opts && opts[:length] && !(opts[:length] === el.size) if type.first register_sub(name, @generator.from_ruby(el).bind_array { all_elements type.first }, raise_on_error) elsif blk register_sub(name, @generator.from_ruby(el).bind_array { all_elements &blk }, raise_on_error) else el end else @errors[lookup_name] = generate_error("was not an Array", raise_on_error) el end elsif type == :boolean if allow_nil el.nil? ? nil : DataBindings.true_boolean?(el) else DataBindings.true_boolean?(el) end elsif Symbol === type if el.is_a?(Hash) register_sub(name, @generator.from_ruby(el).bind(type), raise_on_error) else @errors[lookup_name] = generate_error("was not a Hash", raise_on_error) el end else raise "Unknown type #{type.inspect}" end if inclusion = opts && opts[:in] @errors[lookup_name] = generate_error("was not included in #{inclusion.inspect}", raise_on_error) unless inclusion.include?(new_el) end @errors[lookup_name] = generate_error("was nil", raise_on_error) if new_el.nil? && !allow_nil new_el end end |
#pre_convert ⇒ Object
46 47 48 |
# File 'lib/data_bindings/bound.rb', line 46 def pre_convert valid! end |
#valid! ⇒ Object
41 42 43 44 |
# File 'lib/data_bindings/bound.rb', line 41 def valid! valid? or raise FailedValidation.new("Object was invalid with the following errors: #{errors.join(", ")}", errors, source) self end |
#valid? ⇒ Boolean
27 28 29 30 |
# File 'lib/data_bindings/bound.rb', line 27 def valid? calculate_validness errors.valid? end |