Class: CouchRest::Model::Property
- Inherits:
-
Object
- Object
- CouchRest::Model::Property
- Includes:
- Typecast
- Defined in:
- lib/couchrest/model/property.rb
Instance Attribute Summary collapse
-
#alias ⇒ Object
readonly
Returns the value of attribute alias.
-
#allow_blank ⇒ Object
readonly
Returns the value of attribute allow_blank.
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#casted ⇒ Object
readonly
Returns the value of attribute casted.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#init_method ⇒ Object
readonly
Returns the value of attribute init_method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#read_only ⇒ Object
readonly
Returns the value of attribute read_only.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#build(*args) ⇒ Object
Initialize a new instance of a property’s type ready to be used.
-
#cast(parent, value) ⇒ Object
Cast the provided value using the properties details.
-
#cast_value(parent, value) ⇒ Object
Cast an individual value.
- #default_value ⇒ Object
-
#initialize(name, options = {}, &block) ⇒ Property
constructor
Attribute to define.
- #to_s ⇒ Object
- #to_sym ⇒ Object
Methods included from Typecast
Constructor Details
#initialize(name, options = {}, &block) ⇒ Property
Attribute to define. All Properties are assumed casted unless the type is nil.
11 12 13 14 15 16 |
# File 'lib/couchrest/model/property.rb', line 11 def initialize(name, = {}, &block) @name = name.to_s () parse_type(, &block) self end |
Instance Attribute Details
#alias ⇒ Object (readonly)
Returns the value of attribute alias.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def alias @alias end |
#allow_blank ⇒ Object (readonly)
Returns the value of attribute allow_blank.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def allow_blank @allow_blank end |
#array ⇒ Object (readonly)
Returns the value of attribute array.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def array @array end |
#casted ⇒ Object (readonly)
Returns the value of attribute casted.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def casted @casted end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def default @default end |
#init_method ⇒ Object (readonly)
Returns the value of attribute init_method.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def init_method @init_method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def @options end |
#read_only ⇒ Object (readonly)
Returns the value of attribute read_only.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def read_only @read_only end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def type @type end |
Instance Method Details
#build(*args) ⇒ Object
Initialize a new instance of a property’s type ready to be used. If a proc is defined for the init method, it will be used instead of a normal call to the class.
73 74 75 76 77 78 79 80 81 |
# File 'lib/couchrest/model/property.rb', line 73 def build(*args) raise StandardError, "Cannot build property without a class" if @type.nil? if @init_method.is_a?(Proc) @init_method.call(*args) else @type.send(@init_method, *args) end end |
#cast(parent, value) ⇒ Object
Cast the provided value using the properties details.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/couchrest/model/property.rb', line 27 def cast(parent, value) return value unless casted if array if value.nil? value = [] elsif value.is_a?(Hash) # Assume provided as a params hash where key is index value = parameter_hash_to_array(value) elsif !value.is_a?(Array) raise "Expecting an array or keyed hash for property #{parent.class.name}##{self.name}" end arr = value.collect { |data| cast_value(parent, data) } arr.reject!{ |data| data.nil? } unless allow_blank # allow casted_by calls to be passed up chain by wrapping in CastedArray CastedArray.new(arr, self, parent) elsif (type == Object || type == Hash) && (value.is_a?(Hash)) # allow casted_by calls to be passed up chain by wrapping in CastedHash CastedHash[value, self, parent] elsif !value.nil? cast_value(parent, value) end end |
#cast_value(parent, value) ⇒ Object
Cast an individual value
51 52 53 54 55 56 57 58 |
# File 'lib/couchrest/model/property.rb', line 51 def cast_value(parent, value) if !allow_blank && value.to_s.empty? nil else value = typecast_value(parent, self, value) associate_casted_value_to_parent(parent, value) end end |
#default_value ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/couchrest/model/property.rb', line 60 def default_value return if default.nil? if default.class == Proc default.call else # TODO identify cause of mutex errors Marshal.load(Marshal.dump(default)) end end |
#to_s ⇒ Object
18 19 20 |
# File 'lib/couchrest/model/property.rb', line 18 def to_s name end |
#to_sym ⇒ Object
22 23 24 |
# File 'lib/couchrest/model/property.rb', line 22 def to_sym @_sym_name ||= name.to_sym end |