Module: Top4R::ModelMixin::ClassMethods

Defined in:
lib/top4r/model.rb

Instance Method Summary collapse

Instance Method Details

#default_private_fieldsObject



7
# File 'lib/top4r/model.rb', line 7

def default_private_fields; []; end

#default_public_fieldsObject



5
# File 'lib/top4r/model.rb', line 5

def default_public_fields; []; end

#fieldsObject



30
31
32
# File 'lib/top4r/model.rb', line 30

def fields
  (self.default_public_fields + self.default_private_fields).uniq.join(',')
end

#unmarshal(raw) ⇒ Object

Unmarshal object singular or plural array of model objects from JSON serialization. Currently JSON is only supported.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/top4r/model.rb', line 11

def unmarshal(raw)
  # input = JSON.parse(raw)
  input = raw
  # puts "\ninput: #{input.inspect}"
  def unmarshal_model(hash)
    mine = self.new(hash)
    # puts "\n mine: #{mine.inspect}"
    mine.unmarshal_other_attrs if mine.respond_to? :unmarshal_other_attrs
    mine
  end
  return unmarshal_model(input) if input.is_a?(Hash) # singular case
  result = []
  input.each do |hash|
    model = unmarshal_model(hash) if hash.is_a?(Hash)
    result << model
  end if input.is_a?(Array)
  result # plural case
end

#unmarshal_model(hash) ⇒ Object

puts “ninput: #Top4R::ModelMixin::ClassMethods.inputinput.inspect”



15
16
17
18
19
20
# File 'lib/top4r/model.rb', line 15

def unmarshal_model(hash)
  mine = self.new(hash)
  # puts "\n mine: #{mine.inspect}"
  mine.unmarshal_other_attrs if mine.respond_to? :unmarshal_other_attrs
  mine
end