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.
-
#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.
-
#type_class ⇒ Object
readonly
Returns the value of attribute type_class.
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, type = nil, options = {}) ⇒ Property
constructor
Attribute to define.
- #to_s ⇒ Object
Methods included from Typecast
Constructor Details
#initialize(name, type = nil, options = {}) ⇒ Property
Attribute to define. All Properties are assumed casted unless the type is nil.
11 12 13 14 15 16 17 |
# File 'lib/couchrest/model/property.rb', line 11 def initialize(name, type = nil, = {}) @name = name.to_s @casted = true parse_type(type) () 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 |
#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 |
#type_class ⇒ Object (readonly)
Returns the value of attribute type_class.
7 8 9 |
# File 'lib/couchrest/model/property.rb', line 7 def type_class @type_class 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.
65 66 67 68 69 70 71 72 |
# File 'lib/couchrest/model/property.rb', line 65 def build(*args) raise StandardError, "Cannot build property without a class" if @type_class.nil? if @init_method.is_a?(Proc) @init_method.call(*args) else @type_class.send(@init_method, *args) end end |
#cast(parent, value) ⇒ Object
Cast the provided value using the properties details.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/couchrest/model/property.rb', line 24 def cast(parent, value) return value unless casted if type.is_a?(Array) if value.nil? value = [] elsif [Hash, HashWithIndifferentAccess].include?(value.class) # 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) } # 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
47 48 49 50 |
# File 'lib/couchrest/model/property.rb', line 47 def cast_value(parent, value) value = typecast_value(value, self) associate_casted_value_to_parent(parent, value) end |
#default_value ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/couchrest/model/property.rb', line 52 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
19 20 21 |
# File 'lib/couchrest/model/property.rb', line 19 def to_s name end |