Class: Dymos::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Callbacks, ActiveModel::Dirty, ActiveModel::Model, Persistence
Defined in:
lib/dymos/model.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from Persistence

#new_record

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistence

#delete, #destroyed?, #new_record?, #persisted?, #save, #save!, #update, #update!

Constructor Details

#initialize(params = {}) ⇒ Model

Returns a new instance of Model.



14
15
16
17
18
# File 'lib/dymos/model.rb', line 14

def initialize(params={})
  @attributes={}
  send :attributes=, params, true
  super
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/dymos/model.rb', line 34

def method_missing(name, *args, &block)

  if Dymos.model_update_query_methods.include? name
    @query||={}
    @query[name]=args
    self
  else
    super
  end
end

Class Attribute Details

.last_execute_queryObject

Returns the value of attribute last_execute_query.



21
22
23
# File 'lib/dymos/model.rb', line 21

def last_execute_query
  @last_execute_query
end

Instance Attribute Details

#last_execute_queryObject

Returns the value of attribute last_execute_query.



10
11
12
# File 'lib/dymos/model.rb', line 10

def last_execute_query
  @last_execute_query
end

#metadataObject

Returns the value of attribute metadata.



10
11
12
# File 'lib/dymos/model.rb', line 10

def 
  @metadata
end

Class Method Details

._execute(builder) ⇒ Object



149
150
151
152
153
154
# File 'lib/dymos/model.rb', line 149

def self._execute(builder)
  query = builder.build
  response = ::Dymos::Client.new.command builder.command, query
  @last_execute_query = {command: builder.command, query: query}
  to_model(class_name, response)
end

.allObject



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dymos/model.rb', line 122

def self.all
  if @query.present? && (@query.keys & [:conditions, :add_condition, :where]).present?
    builder = ::Dymos::Query::Query.new.name(table_name)
  else
    builder = ::Dymos::Query::Scan.new.name(table_name)
  end
  @query.each do |k, v|
    builder.send k, *v
  end if @query.present?
  @query={}
  _execute(builder)
end

.class_nameString

Returns:

  • (String)


177
178
179
# File 'lib/dymos/model.rb', line 177

def self.class_name
  self.name
end

.describeObject



164
165
166
167
# File 'lib/dymos/model.rb', line 164

def self.describe
  builder=::Dymos::Query::Describe.new.name(table_name)
  ::Dymos::Client.new.command :describe_table, builder.build
end

.field(attr, type, default: nil, desc: nil) ⇒ Object



45
46
47
48
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
# File 'lib/dymos/model.rb', line 45

def self.field(attr, type, default: nil, desc: nil)
  fail StandardError('attribute name is invalid') if attr =~ /[\!\?]$/
  fail StandardError('require "default" option') if (type == :boolean && default.nil?)

  @fields ||= {}
  @fields[attr]={
    type: type,
    default: default,
    desc:desc
  }
  define_attribute_methods attr

  define_model_callbacks attr
  define_method(attr) { |raw=false|
    run_callbacks attr do
      val = read_attribute(attr) || default
      return val if raw || !val.present?
      case type
        when :boolean
          to_b(val)
        when :time
          Time.parse val rescue nil
        when :integer
          val.to_i
        else
          val
      end
    end

  }
  define_singleton_method("#{attr}_type") { type }
  define_method("#{attr}_type") { type }
  define_singleton_method("#{attr}_desc") { desc }
  define_method("#{attr}_desc") { desc }
  define_method("#{attr}?") do
    val = self.send attr
    if type == :boolean
      val
    else
      !val.nil?
    end
  end

  define_model_callbacks :"set_#{attr}"
  define_method("#{attr}=") do |value, initialize=false|
    run_callbacks :"set_#{attr}" do
      value = value.iso8601 if self.class.fields.include?(attr) && value.is_a?(Time)
      write_attribute(attr, value, initialize)
    end
  end
end

.fieldsObject



97
98
99
# File 'lib/dymos/model.rb', line 97

def self.fields
  @fields
end

.find(key1, key2 = nil) ⇒ Object



140
141
142
143
144
145
146
147
148
# File 'lib/dymos/model.rb', line 140

def self.find(key1, key2=nil)
  indexes = key_scheme
  keys={}
  keys[indexes.first[:attribute_name].to_sym] = key1
  keys[indexes.last[:attribute_name].to_sym] = key2 if indexes.size > 1

  builder = ::Dymos::Query::GetItem.new.name(table_name).key(keys)
  _execute(builder)
end

.key_schemeObject



156
157
158
# File 'lib/dymos/model.rb', line 156

def self.key_scheme
  @key_scheme ||= describe[:table][:key_schema]
end

.method_missing(name, *args, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/dymos/model.rb', line 23

def method_missing(name, *args, &block)
  if Dymos.model_query_methods.include? name
    @query||={}
    @query[name]=args
    self
  else
    super
  end
end

.oneObject



135
136
137
138
# File 'lib/dymos/model.rb', line 135

def self.one
  @query[:limit] = 1
  self.all.first
end

.table(name) ⇒ Object



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

def self.table(name)
  define_singleton_method('table_name') { name }
  define_method('table_name') { name }
end

Instance Method Details

#attributes(raw = false) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/dymos/model.rb', line 114

def attributes(raw=false)
  attrs = {}
  self.class.fields.keys.each do |name|
    attrs[name] = send "#{name}", raw if respond_to? "#{name}"
  end
  attrs
end

#attributes=(attributes = {}, initialize = false) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/dymos/model.rb', line 106

def attributes=(attributes = {}, initialize = false)
  if attributes
    attributes.each do |attr, value|
      send("#{attr}=", value, initialize) if respond_to? "#{attr}="
    end
  end
end

#class_nameString

Returns:

  • (String)


182
183
184
# File 'lib/dymos/model.rb', line 182

def class_name
  self.class.name
end

#indexesObject



169
170
171
172
173
174
# File 'lib/dymos/model.rb', line 169

def indexes
  scheme = self.class.key_scheme.map do |scheme|
    [scheme[:attribute_name], send(scheme[:attribute_name])]
  end
  scheme.to_h
end

#reload!Object



160
161
162
# File 'lib/dymos/model.rb', line 160

def reload!
  reset_changes
end