Class: FieldMapper::Standard::Plat

Inherits:
Object
  • Object
show all
Extended by:
NameHelper
Includes:
Marshaller, NameHelper
Defined in:
lib/field_mapper/standard/plat.rb

Direct Known Subclasses

Custom::Plat

Constant Summary

Constants included from Marshaller

Marshaller::OPTIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NameHelper

attr_name

Methods included from Marshaller

#marshal, #unmarshal

Constructor Details

#initialize(params = {}) ⇒ Plat

Returns a new instance of Plat.



74
75
76
77
78
# File 'lib/field_mapper/standard/plat.rb', line 74

def initialize(params={})
  @node_id = params["_node_id"]
  assign_defaults
  assign_params params
end

Instance Attribute Details

#node_idObject (readonly)

Returns the value of attribute node_id.



72
73
74
# File 'lib/field_mapper/standard/plat.rb', line 72

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_namesObject



20
21
22
# File 'lib/field_mapper/standard/plat.rb', line 20

def field_names
  @field_names ||= {}
end

.fieldsObject



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

Returns:

  • (Boolean)


57
58
59
# File 'lib/field_mapper/standard/plat.rb', line 57

def has_plat_fields?
  !plat_fields.empty?
end

.plat_fieldsObject



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

Instance Method Details

#[](field_name) ⇒ Object

Raises:



95
96
97
98
# File 'lib/field_mapper/standard/plat.rb', line 95

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

Raises:



100
101
102
103
104
# File 'lib/field_mapper/standard/plat.rb', line 100

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



91
92
93
# File 'lib/field_mapper/standard/plat.rb', line 91

def after_convert(from: nil, to: nil)
  # abstract method to be implemented by subclasses
end

#cache_keyObject



177
178
179
# File 'lib/field_mapper/standard/plat.rb', line 177

def cache_key
  self.class.name + "-" + Digest::MD5.hexdigest(to_hash.to_s)
end

#scopeObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/field_mapper/standard/plat.rb', line 80

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, placeholders: false) ⇒ Object



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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/field_mapper/standard/plat.rb', line 106

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: 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: 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 include_meta
    hash = {
      _node_id: object_id,
      _flat: flatten
    }.merge(hash)
  end

  HashWithIndifferentAccess.new(hash)
end