Class: Moo::Model::Side

Inherits:
Object show all
Defined in:
lib/moo/model/side.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Side

Returns a new instance of Side.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
# File 'lib/moo/model/side.rb', line 7

def initialize
  @data = []
  yield self if block_given?
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



5
6
7
# File 'lib/moo/model/side.rb', line 5

def data
  @data
end

#packObject

Returns the value of attribute pack.



5
6
7
# File 'lib/moo/model/side.rb', line 5

def pack
  @pack
end

#side_numObject

Returns the value of attribute side_num.



5
6
7
# File 'lib/moo/model/side.rb', line 5

def side_num
  @side_num
end

#templateObject

Returns the value of attribute template.



5
6
7
# File 'lib/moo/model/side.rb', line 5

def template
  @template
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/moo/model/side.rb', line 5

def type
  @type
end

Instance Method Details

#data_from_hash(hash) ⇒ Object



63
64
65
66
67
68
# File 'lib/moo/model/side.rb', line 63

def data_from_hash(hash)
  klass = hash[:type].gsub(/\b\w/){|s|s.upcase} 
  data = eval(klass).new
  data.from_hash hash
  data
end

#from_hash(hash) ⇒ Object



56
57
58
59
60
61
# File 'lib/moo/model/side.rb', line 56

def from_hash hash
  self.side_num = hash[:sideNum]
  self.template_code = hash[:templateCode]
  self.type = hash[:type]
  self.data = hash[:data].map {|d| data_from_hash  d }
end

#from_json(json) ⇒ Object



52
53
54
# File 'lib/moo/model/side.rb', line 52

def from_json json
  from_hash(JSON.parse(json, :symbolize_names => true))
end

#template_codeObject



34
35
36
37
# File 'lib/moo/model/side.rb', line 34

def template_code
  return nil if @template.nil?
  @template.code
end

#template_code=(code) ⇒ Object

wrapper that sets template object



27
28
29
30
31
32
# File 'lib/moo/model/side.rb', line 27

def template_code=code
  unless Template.codes.include? code
    raise ArgumentError, "invalid templatecode"
  end
  @template = Template.with_code code
end

#to_hashObject



43
44
45
46
47
48
49
50
# File 'lib/moo/model/side.rb', line 43

def to_hash
  hash = {
    sideNum:  side_num,
    templateCode:  template_code,
    type: type,
    data: data.map { |d| d.to_hash }
  }
end

#to_jsonObject



39
40
41
# File 'lib/moo/model/side.rb', line 39

def to_json
  to_hash.to_json
end