Class: FieldMapper::Standard::Plat
- Inherits:
-
Object
- Object
- FieldMapper::Standard::Plat
- Extended by:
- NameHelper
- Includes:
- Marshaller, NameHelper
- Defined in:
- lib/field_mapper/standard/plat.rb
Direct Known Subclasses
Constant Summary
Constants included from Marshaller
Instance Attribute Summary collapse
-
#node_id ⇒ Object
readonly
Returns the value of attribute node_id.
Class Method Summary collapse
- .field(name, type: nil, desc: nil, default: nil, &block) ⇒ Object
- .field_names ⇒ Object
- .fields ⇒ Object
- .find_field(field_name) ⇒ Object
- .has_plat_fields? ⇒ Boolean
- .plat_fields ⇒ Object
Instance Method Summary collapse
- #[](field_name) ⇒ Object
- #[]=(field_name, value) ⇒ Object
- #after_convert(from: nil, to: nil) ⇒ Object
- #cache_key ⇒ Object
-
#initialize(params = {}) ⇒ Plat
constructor
A new instance of Plat.
- #scope ⇒ Object
- #to_hash(flatten: false, history: {}, include_meta: true) ⇒ Object
Methods included from NameHelper
Methods included from Marshaller
Constructor Details
#initialize(params = {}) ⇒ Plat
Returns a new instance of Plat.
72 73 74 75 76 |
# File 'lib/field_mapper/standard/plat.rb', line 72 def initialize(params={}) @node_id = params["_node_id"] assign_defaults assign_params params end |
Instance Attribute Details
#node_id ⇒ Object (readonly)
Returns the value of attribute node_id.
70 71 72 |
# File 'lib/field_mapper/standard/plat.rb', line 70 def node_id @node_id end |
Class Method Details
.field(name, type: nil, desc: nil, default: nil, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/field_mapper/standard/plat.rb', line 24 def field( name, type: nil, desc: nil, default: nil, &block ) field_names[attr_name(name)] = name field = fields[name] = FieldMapper::Standard::Field.new( name, type: type, desc: desc, default: default ) field.instance_exec(&block) if block_given? define_method(attr_name name) do self[name] end define_method("#{attr_name name}=") do |value| self[name] = value end end |
.field_names ⇒ Object
20 21 22 |
# File 'lib/field_mapper/standard/plat.rb', line 20 def field_names @field_names ||= {} end |
.fields ⇒ Object
16 17 18 |
# File 'lib/field_mapper/standard/plat.rb', line 16 def fields @fields ||= HashWithIndifferentAccess.new end |
.find_field(field_name) ⇒ Object
51 52 53 |
# File 'lib/field_mapper/standard/plat.rb', line 51 def find_field(field_name) fields[field_names[attr_name(field_name)]] end |
.has_plat_fields? ⇒ Boolean
55 56 57 |
# File 'lib/field_mapper/standard/plat.rb', line 55 def has_plat_fields? !plat_fields.empty? end |
.plat_fields ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/field_mapper/standard/plat.rb', line 59 def plat_fields fields.reduce({}) do |memo, keypair| if keypair.last.plat_field? memo[keypair.first] = keypair.last end memo end end |
Instance Method Details
#[](field_name) ⇒ Object
93 94 95 96 |
# File 'lib/field_mapper/standard/plat.rb', line 93 def [](field_name) raise FieldNotDefined unless field_exists?(field_name) instance_variable_get "@#{attr_name(field_name)}" end |
#[]=(field_name, value) ⇒ Object
98 99 100 101 102 |
# File 'lib/field_mapper/standard/plat.rb', line 98 def []=(field_name, value) field = self.class.find_field(field_name) raise FieldNotDefined if field.nil? instance_variable_set "@#{attr_name(field_name)}", field.cast(value) end |
#after_convert(from: nil, to: nil) ⇒ Object
89 90 91 |
# File 'lib/field_mapper/standard/plat.rb', line 89 def after_convert(from: nil, to: nil) # abstract method to be implemented by subclasses end |
#cache_key ⇒ Object
163 164 165 |
# File 'lib/field_mapper/standard/plat.rb', line 163 def cache_key self.class.name + "-" + Digest::MD5.hexdigest(to_hash.to_s) end |
#scope ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/field_mapper/standard/plat.rb', line 78 def scope @scope ||= begin scope_name = self.class.name.split("::")[0..-2].join("::") if scope_name.empty? ::Object else ::Object.const_get(scope_name) end end end |
#to_hash(flatten: false, history: {}, include_meta: true) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/field_mapper/standard/plat.rb', line 104 def to_hash(flatten: false, history: {}, include_meta: true) history[object_id] = true hash = self.class.fields.values.reduce(HashWithIndifferentAccess.new) do |memo, field| name = field.name value = instance_variable_get("@#{attr_name(name)}") if value.present? case field.type.name when "FieldMapper::Types::Plat" then if value.is_a? FieldMapper::Standard::Plat oid = value.object_id if history[oid].nil? history[oid] = true value = value.to_hash(flatten: flatten, history: history, include_meta: ) value = marshal(value) if flatten else value = oid end else value end when "FieldMapper::Types::List" then if field.plat_list? value = value.map do |val| if val.is_a? FieldMapper::Standard::Plat oid = val.object_id if history[oid].nil? history[oid] = true val.to_hash(flatten: flatten, history: history, include_meta: ) else oid end else val end end end value = marshal(value) if flatten when "Money" then value = value.format(with_currency: true) when "Time" then value = value.utc.iso8601 end end memo[name] = value memo end if hash = { _node_id: object_id, _flat: flatten }.merge(hash) end HashWithIndifferentAccess.new(hash) end |