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



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dymos/model.rb', line 37

def method_missing(name, *args, &block)
  methods ||= ::Dymos::Query::UpdateItem.instance_methods(false)+
    ::Dymos::Query::PutItem.instance_methods(false)+
    ::Dymos::Query::DeleteItem.instance_methods(false)
  if 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 
  
end

Class Method Details

._execute(builder) ⇒ Object



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

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



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/dymos/model.rb', line 127

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)


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

def self.class_name
  self.name
end

.describeObject



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

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



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
# File 'lib/dymos/model.rb', line 50

def self.field(attr, type, default: nil, desc: nil)
  fail StandardError('attribute name is invalid') if attr =~ /[\!\?]$/
  fail StandardError('require "default" option') if (type == :bool && 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 :bool
          to_b(val)
        when :time
          Time.parse val
        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 == :bool
      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



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

def self.fields
  @fields
end

.find(key1, key2 = nil) ⇒ Object



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

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



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

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
32
33
34
# File 'lib/dymos/model.rb', line 23

def method_missing(name, *args, &block)
  methods ||= ::Dymos::Query::Query.instance_methods(false)+
    ::Dymos::Query::GetItem.instance_methods(false)+
    ::Dymos::Query::Scan.instance_methods(false)
  if methods.include? name
    @query||={}
    @query[name]=args
    self
  else
    super
  end
end

.oneObject



140
141
142
143
# File 'lib/dymos/model.rb', line 140

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

.table(name) ⇒ Object



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

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

Instance Method Details

#attributes(raw = false) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/dymos/model.rb', line 119

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



111
112
113
114
115
116
117
# File 'lib/dymos/model.rb', line 111

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)


187
188
189
# File 'lib/dymos/model.rb', line 187

def class_name
  self.class.name
end

#indexesObject



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

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

#reload!Object



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

def reload!
  reset_changes
end