Class: Diva::Model
- Inherits:
-
Object
- Object
- Diva::Model
- Extended by:
- ModelExtend
- Includes:
- Comparable
- Defined in:
- lib/diva/model.rb
Instance Attribute Summary
Attributes included from ModelExtend
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#[]=(key, value) ⇒ Object
カラムに別の値を格納する。 格納後、データはDataSourceに保存される.
- #deconstruct_keys(keys) ⇒ Object
- #dig(key, *args) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#fetch(key) ⇒ Object
(also: #[])
カラムの生の内容を返す.
-
#filtering ⇒ Object
キーとして定義されていない値を全て除外した配列を生成して返す。 また、Modelを子に含んでいる場合、それを外部キーに変換する。.
- #hash ⇒ Object
-
#initialize(args) ⇒ Model
constructor
A new instance of Model.
-
#me?(service = nil) ⇒ Boolean
このModelが、登録されているアカウントのうちいずれかが作成したものであれば true を返す ==== Args [service] Service | Enumerable 「自分」のService ==== Return [true] 自分のによって作られたオブジェクトである [false] 自分のによって作られたオブジェクトではない.
-
#merge(other) ⇒ Object
データをマージする。 selfにあってotherにもあるカラムはotherの内容で上書きされる。 上書き後、データはDataSourceに保存される.
-
#perma_link ⇒ Object
このModelのパーマリンクを返す。 パーマリンクはWebのURLで、Web上のリソースでない場合はnilを返す。 ==== Return 次のいずれか [URI::HTTP|Diva::URI] パーマリンク [nil] パーマリンクが存在しない.
-
#title ⇒ Object
このインスタンスのタイトル。.
- #to_h ⇒ Object
- #to_json(*rest) ⇒ Object
-
#uri ⇒ Object
このModelのURIを返す。 ==== Return [URI::Generic|Diva::URI] パーマリンク.
-
#validate ⇒ Object
カラムと型が違うものがある場合、例外を発生させる。.
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.
17 18 19 20 21 |
# File 'lib/diva/model.rb', line 17 def initialize(args) @value = args.dup validate self.class.store_datum(self) end |
Instance Method Details
#<=>(other) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/diva/model.rb', line 63 def <=>(other) if other.is_a?(Diva::Model) created - other.created elsif other.respond_to?(:[]) && other[:created] created - other[:created] else id - other end end |
#==(other) ⇒ Object
73 74 75 76 77 |
# File 'lib/diva/model.rb', line 73 def ==(other) if other.is_a? Diva::Model self.class == other.class && uri == other.uri end end |
#[]=(key, value) ⇒ Object
カラムに別の値を格納する。 格納後、データはDataSourceに保存される
99 100 101 102 |
# File 'lib/diva/model.rb', line 99 def []=(key, value) @value[key.to_sym] = value self.class.store_datum(self) end |
#deconstruct_keys(keys) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/diva/model.rb', line 149 def deconstruct_keys(keys) if keys @value.slice(*keys) # 高速化のため、fetchを呼び出していない else @value end end |
#dig(key, *args) ⇒ Object
139 140 141 142 143 144 145 146 147 |
# File 'lib/diva/model.rb', line 139 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
79 80 81 |
# File 'lib/diva/model.rb', line 79 def eql?(other) self == other end |
#fetch(key) ⇒ Object Also known as: []
カラムの生の内容を返す
92 93 94 |
# File 'lib/diva/model.rb', line 92 def fetch(key) @value[key.to_sym] end |
#filtering ⇒ Object
キーとして定義されていない値を全て除外した配列を生成して返す。 また、Modelを子に含んでいる場合、それを外部キーに変換する。
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/diva/model.rb', line 116 def filtering datum = to_h result = {} self.class.fields.each do |field| result[field.name] = field.type.cast(datum[field.name]) rescue Diva::InvalidTypeError => exception raise Diva::InvalidTypeError, "#{exception} in field `#{field}'" end result end |
#hash ⇒ Object
59 60 61 |
# File 'lib/diva/model.rb', line 59 def hash @_hash ||= uri.to_s.hash ^ self.class.hash end |
#me?(service = nil) ⇒ Boolean
このModelが、登録されているアカウントのうちいずれかが作成したものであれば true を返す
Args
- service
-
Service | Enumerable 「自分」のService
Return
- true
-
自分のによって作られたオブジェクトである
- false
-
自分のによって作られたオブジェクトではない
55 56 57 |
# File 'lib/diva/model.rb', line 55 def me?(service=nil) false end |
#merge(other) ⇒ Object
データをマージする。 selfにあってotherにもあるカラムはotherの内容で上書きされる。 上書き後、データはDataSourceに保存される
26 27 28 29 30 |
# File 'lib/diva/model.rb', line 26 def merge(other) @value.update(other.to_h) validate self.class.store_datum(self) end |
#perma_link ⇒ Object
このModelのパーマリンクを返す。 パーマリンクはWebのURLで、Web上のリソースでない場合はnilを返す。
Return
次のいずれか
- URI::HTTP|Diva::URI
-
パーマリンク
- nil
-
パーマリンクが存在しない
38 39 40 |
# File 'lib/diva/model.rb', line 38 def perma_link nil end |
#title ⇒ Object
このインスタンスのタイトル。
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/diva/model.rb', line 128 def title fields = self.class.fields.lazy.map(&:name) if fields.include?(:name) name.gsub("\n", '') elsif fields.include?(:description) description.gsub("\n", '') else to_s.gsub("\n", '') end end |
#to_h ⇒ Object
83 84 85 |
# File 'lib/diva/model.rb', line 83 def to_h self.class.fields.to_h { |f| [f.name, fetch(f.name)] } end |
#to_json(*rest) ⇒ Object
87 88 89 |
# File 'lib/diva/model.rb', line 87 def to_json(*rest) self.class.fields.to_h { |f| [f.name, f.dump_for_json(fetch(f.name))] }.to_json(*rest) end |
#uri ⇒ Object
このModelのURIを返す。
Return
- URI::Generic|Diva::URI
-
パーマリンク
45 46 47 |
# File 'lib/diva/model.rb', line 45 def uri perma_link || Diva::URI.new("#{self.class.scheme}://#{self.class.host}#{path}") end |
#validate ⇒ Object
カラムと型が違うものがある場合、例外を発生させる。
105 106 107 108 109 110 111 112 |
# File 'lib/diva/model.rb', line 105 def validate raise "argument is #{@value}, not Hash" unless @value.is_a?(Hash) self.class.fields.each do |field| @value[field.name] = field.type.cast(@value[field.name]) rescue Diva::InvalidTypeError => exception raise Diva::InvalidTypeError, "#{exception} in field `#{field}'" end end |