Module: Mongous::Document

Defined in:
lib/mongous/document.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, **_opts, &_block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/mongous/document.rb', line 18

def method_missing( sym, *args, **_opts, &_block )
  m  =  /\A(\w+)([=])?\Z/.match( sym.to_s )
  if  args.size == 0 && m[2].nil?
    self[ m[1] ]
  elsif  args.size == 1 && !m[2].nil?
    self[ m[1] ]  =  args.shift
  else
    raise  Mongous::Error, "invalid parameter. #{sym} #{args.join(', ')}"
  end
end

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/mongous/document.rb', line 5

def included( klass )
  klass.extend  Extention
end

Instance Method Details

#[](label) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mongous/document.rb', line 114

def []( label )
  label  =  label.to_s

  if  self.class.symbols[:strict]
    labels  =  ["_id"] + self.class.fields.keys
    if  !labels.include?( label )
      raise  Mongous::Error, "undefined field. : #{ label }"
    end
  end

  return  @doc[label]    if @doc.has_key?(label)

  field  =  self.class.fields[label]
  return  nil    if field.nil?

  case  default  =  field[:default]
  when  Proc
    self.instance_eval( &default )
  else
    default
  end
end

#[]=(label, value) ⇒ Object



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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mongous/document.rb', line 137

def []=( label, value )
  label  =  label.to_s
  if  self.class.symbols[:strict]
    labels  =  ["_id"] + self.class.fields.keys
    if  !labels.include?( label )
      raise  Mongous::Error, "undefined field. : #{ label }"
    end
  end

  field  =  self.class.fields[label]
  if field.nil?
    @doc[label]  =  value
    return
  end

  types  =  []
  if ( attrs  =  field[:_attrs]  ||  [] )
    attrs.each do |attr|
      if  attr.class == Class
        types  <<  attr
        attrs  -=  [attr]
        break
      end
    end
  end

  if  !types.empty?
    if  !(attrs & [:must, :not_null]).empty?
      if  !types.include?( value.class )
        raise  Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
      end
    else
      if  !(types + [NilClass] ).include?( value.class )
        raise  Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
      end
    end
  else
    if  !(attrs & [:must, :not_null]).empty?
      if  [NilClass].include?( value.class )
        raise  Mongous::Error, "invalid type. : #{ label } : #{ value.class }"
      end
    end
  end

  @doc[label]  =  value

  attrs.each do |attr|
    case  attr
    when  Proc
      if  !self.instance_eval( &attr )
        raise  Mongous::Error, "violation detected. : #{ label } : #{ value }"
      end
    when  Array
      if  !attr.include?( value )
        raise  Mongous::Error, "not include. : #{ label } : #{ value }"
      end
    when  Range
      if  !attr.cover?( value )
        raise  Mongous::Error, "out of range. : #{ label } : #{ value }"
      end
    when  Regexp
      if  !attr.match( value )
        raise  Mongous::Error, "unmatch regexp. : #{ label } : #{ value }"
      end
    end
  end
end

#getvalue_or_callproc(value_or_proc) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/mongous/document.rb', line 40

def getvalue_or_callproc( value_or_proc )
  case  value_or_proc
  when  Proc
    value_or_proc.call
  else
    value_or_proc
  end
end

#having?(label) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
# File 'lib/mongous/document.rb', line 29

def having?( label )
  case  label
  when  NilClass
    false
  when  String
    !label.strip.empty?
  else
    true
  end
end

#initialize(**doc) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/mongous/document.rb', line 10

def initialize( **doc )
  @doc  =  {}
  doc.each do |label, value|
    label  =  label.to_s
    @doc[label]  =  value
  end
end

#saveObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/mongous/document.rb', line 49

def save
  if  @doc["_id"].nil? || self.class.collection.find( { "_id"=> @doc["_id"] } ).count == 0
    savemode  =  :create
  else
    savemode  =  :update
  end

  self.class.fields.each do |label, field|
    default_value  =  getvalue_or_callproc( field[:default] )
    must  =  field[:_attrs].include?(:must)
    if  @doc.has_key?(label)
      if  !having?( @doc[label] )
        if  default_value
          self[label]  =  default_value
        elsif  must
          raise  Mongous::Error, "must but unassigned field. : #{ label }"
        elsif  self.class.symbols[:strict]
          self[label]  =  nil
        end
      end
    else
      if  default_value
        self[label]  =  default_value
      elsif  must
        raise  Mongous::Error, "must but unassigned field. : #{ label }"
      elsif  self.class.symbols[:strict]
        self[label]  =  nil
      end
    end

    case  savemode
    when  :create
      if ( create_value  =  getvalue_or_callproc( field[:create] ) )
        self[label]  =  create_value
      end
    when  :update
      if ( update_value  =  getvalue_or_callproc( field[:update] ) )
        self[label]  =  update_value
      end
    end
  end

  self.class.blocks.each do |call_from, block|
    if  !self.instance_eval( &block )
      raise  Mongous::Error, "violation detected on save. : #{ call_from }"
    end
  end

  case  savemode
  when  :create
    self.class.collection.insert_one( @doc )
  when  :update
    self.class.collection.update_one( { "_id"=> @doc["_id"] }, { '$set' => @doc } )
  end
  self
end

#to_hashObject



106
107
108
# File 'lib/mongous/document.rb', line 106

def to_hash
  @doc.dup
end

#to_json(**opts) ⇒ Object



110
111
112
# File 'lib/mongous/document.rb', line 110

def to_json( **opts )
  ::JSON.generate( @doc, **opts )
end