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, placeholder: nil, &block) ⇒ Object
- .field_names ⇒ Object
- .fields ⇒ Object
- .find_field(field_name) ⇒ Object
- .has_plat_fields? ⇒ Boolean
- .inspect ⇒ Object
- .plat_fields ⇒ Object
- .symbolize_hash(hash) ⇒ 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.
- #inspect ⇒ Object
- #scope ⇒ Object
- #snapshot ⇒ Object
- #to_hash(flatten: false, history: {}, include_meta: true, placeholders: false) ⇒ Object
Methods included from NameHelper
Methods included from Marshaller
Constructor Details
#initialize(params = {}) ⇒ Plat
Returns a new instance of Plat.
94 95 96 97 98 |
# File 'lib/field_mapper/standard/plat.rb', line 94 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.
92 93 94 |
# File 'lib/field_mapper/standard/plat.rb', line 92 def node_id @node_id end |
Class Method Details
.field(name, type: nil, desc: nil, default: nil, placeholder: 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 50 51 |
# File 'lib/field_mapper/standard/plat.rb', line 24 def field( name, type: nil, desc: nil, default: nil, placeholder: nil, &block ) field_names[attr_name(name)] = name field = fields[name] = FieldMapper::Standard::Field.new( name, type: type, desc: desc, default: default, placeholder: placeholder ) 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
53 54 55 |
# File 'lib/field_mapper/standard/plat.rb', line 53 def find_field(field_name) fields[field_names[attr_name(field_name)]] end |
.has_plat_fields? ⇒ Boolean
57 58 59 |
# File 'lib/field_mapper/standard/plat.rb', line 57 def has_plat_fields? !plat_fields.empty? end |
.inspect ⇒ Object
70 71 72 |
# File 'lib/field_mapper/standard/plat.rb', line 70 def inspect "#{name} #{new.snapshot.inspect}" end |
.plat_fields ⇒ Object
61 62 63 64 65 66 67 68 |
# File 'lib/field_mapper/standard/plat.rb', line 61 def plat_fields fields.reduce({}) do |memo, keypair| if keypair.last.plat_field? memo[keypair.first] = keypair.last end memo end end |
.symbolize_hash(hash) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/field_mapper/standard/plat.rb', line 74 def symbolize_hash(hash) hash.reduce({}) do |memo, pair| key = pair.first.intern value = pair.last value = symbolize_hash(value) if value.is_a?(Hash) if value.is_a?(Array) value = value.map do |val| val = symbolize_hash(val) if val.is_a?(Hash) val end end memo[key] = value memo end end |
Instance Method Details
#[](field_name) ⇒ Object
119 120 121 122 |
# File 'lib/field_mapper/standard/plat.rb', line 119 def [](field_name) raise FieldNotDefined.new("#{self.class.name} does not define: #{field_name}") unless field_exists?(field_name) instance_variable_get "@#{attr_name(field_name)}" end |
#[]=(field_name, value) ⇒ Object
124 125 126 127 128 |
# File 'lib/field_mapper/standard/plat.rb', line 124 def []=(field_name, value) field = self.class.find_field(field_name) raise FieldNotDefined.new("#{self.class.name} does not define: #{field_name}") if field.nil? assign_param field_name, cast_value(field, value) end |
#after_convert(from: nil, to: nil) ⇒ Object
115 116 117 |
# File 'lib/field_mapper/standard/plat.rb', line 115 def after_convert(from: nil, to: nil) # abstract method to be implemented by subclasses end |
#cache_key ⇒ Object
205 206 207 |
# File 'lib/field_mapper/standard/plat.rb', line 205 def cache_key self.class.name + "-" + Digest::MD5.hexdigest(to_hash.to_s) end |
#inspect ⇒ Object
100 101 102 |
# File 'lib/field_mapper/standard/plat.rb', line 100 def inspect "#<#{self.class.name}:0x00#{object_id.to_s(16)} #{snapshot.inspect}>" end |
#scope ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/field_mapper/standard/plat.rb', line 104 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 |
#snapshot ⇒ Object
201 202 203 |
# File 'lib/field_mapper/standard/plat.rb', line 201 def snapshot self.class.symbolize_hash(to_hash(include_meta: false)) end |
#to_hash(flatten: false, history: {}, include_meta: true, placeholders: false) ⇒ Object
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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/field_mapper/standard/plat.rb', line 130 def to_hash(flatten: false, history: {}, include_meta: true, placeholders: false) 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: , placeholders: placeholders ) 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: , placeholders: placeholders ) 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 else value = field.placeholder || field.default if placeholders end memo[name] = value memo end if hash = { _node_id: object_id, _flat: flatten }.merge(hash) end HashWithIndifferentAccess.new(hash) end |