Class: Diva::Model

Inherits:
Object
  • Object
show all
Extended by:
ModelExtend
Includes:
Comparable
Defined in:
lib/diva/model.rb

Instance Attribute Summary

Attributes included from ModelExtend

#slug, #spec

Instance Method Summary collapse

Methods included from ModelExtend

add_field, container_class, field, fields, find_by_uri, host, schema, scheme, store_datum

Constructor Details

#initialize(args) ⇒ Model

Returns a new instance of Model.



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

def initialize(args)
  @value = args.dup
  validate
  self.class.store_datum(self)
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/diva/model.rb', line 62

def <=>(other)
  if other.is_a?(Diva::Model)
    created - other.created
  elsif other.respond_to?(:[]) and other[:created]
    created - other[:created]
  else
    id - other
  end
end

#==(other) ⇒ Object



72
73
74
75
76
# File 'lib/diva/model.rb', line 72

def ==(other)
  if other.is_a? Diva::Model
    self.class == other.class && uri == other.uri
  end
end

#[]=(key, value) ⇒ Object

カラムに別の値を格納する。格納後、データはDataSourceに保存される



100
101
102
103
104
# File 'lib/diva/model.rb', line 100

def []=(key, value)
  @value[key.to_sym] = value
  self.class.store_datum(self)
  value
end

#dig(key, *args) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/diva/model.rb', line 146

def dig(key, *args)
  return nil unless key.respond_to?(:to_sym)
  value = fetch(key)
  if value.nil? || args.empty?
    value
  else
    value.dig(*args)
  end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/diva/model.rb', line 78

def eql?(other)
  self == other
end

#fetch(key) ⇒ Object Also known as: []

カラムの生の内容を返す



93
94
95
# File 'lib/diva/model.rb', line 93

def fetch(key)
  @value[key.to_sym]
end

#filteringObject

キーとして定義されていない値を全て除外した配列を生成して返す。また、Modelを子に含んでいる場合、それを外部キーに変換する。



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/diva/model.rb', line 120

def filtering
  datum = self.to_hash
  result = Hash.new
  self.class.fields.each do |field|
    begin
      result[field.name] = field.type.cast(datum[field.name])
    rescue Diva::InvalidTypeError => err
      raise Diva::InvalidTypeError, "#{err} in field `#{field}'"
    end
  end
  result
end

#hashObject



58
59
60
# File 'lib/diva/model.rb', line 58

def hash
  @_hash ||= self.uri.to_s.hash ^ self.class.hash
end

#me?(service = nil) ⇒ Boolean

このModelが、登録されているアカウントのうちいずれかが作成したものであれば true を返す

Args

service

Service | Enumerable 「自分」のService

Return

true

自分のによって作られたオブジェクトである

false

自分のによって作られたオブジェクトではない

Returns:

  • (Boolean)


54
55
56
# File 'lib/diva/model.rb', line 54

def me?(service=nil)
  false
end

#merge(other) ⇒ Object

データをマージする。selfにあってotherにもあるカラムはotherの内容で上書きされる。上書き後、データはDataSourceに保存される



25
26
27
28
29
# File 'lib/diva/model.rb', line 25

def merge(other)
  @value.update(other.to_hash)
  validate
  self.class.store_datum(self)
end

このModelのパーマリンクを返す。パーマリンクはWebのURLで、Web上のリソースでない場合はnilを返す。

Return

次のいずれか

URI::HTTP|Diva::URI

パーマリンク

nil

パーマリンクが存在しない



37
38
39
# File 'lib/diva/model.rb', line 37

def perma_link
  nil
end

#titleObject

このインスタンスのタイトル。



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/diva/model.rb', line 134

def title
  fields = self.class.fields.lazy.map(&:name)
  case
  when fields.include?(:name)
    name.gsub("\n", '')
  when fields.include?(:description)
    description.gsub("\n", '')
  else
    to_s.gsub("\n", '')
  end
end

#to_hashObject



82
83
84
# File 'lib/diva/model.rb', line 82

def to_hash
  Hash[self.class.fields.map{|f| [f.name, fetch(f.name)] }]
end

#to_json(*rest, **kwrest) ⇒ Object



86
87
88
89
90
# File 'lib/diva/model.rb', line 86

def to_json(*rest, **kwrest)
  Hash[
    self.class.fields.map{|f| [f.name, f.dump_for_json(fetch(f.name))] }
  ].to_json(*rest, **kwrest)
end

#uriObject

このModelのURIを返す。

Return

URI::Generic|Diva::URI

パーマリンク



44
45
46
# File 'lib/diva/model.rb', line 44

def uri
  perma_link || Diva::URI.new("#{self.class.scheme}://#{self.class.host}#{path}")
end

#validateObject

カラムと型が違うものがある場合、例外を発生させる。

Raises:

  • (RuntimeError)


107
108
109
110
111
112
113
114
115
116
# File 'lib/diva/model.rb', line 107

def validate
  raise RuntimeError, "argument is #{@value}, not Hash" if not @value.is_a?(Hash)
  self.class.fields.each do |field|
    begin
      @value[field.name] = field.type.cast(@value[field.name])
    rescue Diva::InvalidTypeError => err
      raise Diva::InvalidTypeError, "#{err} in field `#{field}'"
    end
  end
end