Class: Kweerie::BaseObject
- Inherits:
-
Base
- Object
- Base
- Kweerie::BaseObject
show all
- Defined in:
- lib/kweerie/base_object.rb
Class Method Summary
collapse
Methods inherited from Base
all, bind, inherited, sql_content, sql_file_location, sql_path
Class Method Details
.cast_definitions ⇒ Object
26
27
28
|
# File 'lib/kweerie/base_object.rb', line 26
def cast_definitions
@cast_definitions ||= {}
end
|
.cast_select(field, as: nil) ⇒ Object
22
23
24
|
# File 'lib/kweerie/base_object.rb', line 22
def cast_select(field, as: nil)
cast_definitions[field] = as if as
end
|
.generate(attribute_names) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/kweerie/base_object.rb', line 30
def generate(attribute_names)
cast_definitions = self.cast_definitions
Class.new(self) do
include Comparable
include ObjectMethods::Accessors
include ObjectMethods::Comparison
include ObjectMethods::KeyTransformation
include ObjectMethods::Serialization
include ObjectMethods::TypeCasting
attribute_names.each { |name| attr_reader name }
define_method :initialize do |attrs|
@_raw_original_attributes = attrs.dup
@_original_attributes = attrs.each_with_object({}) do |(key, value), hash|
type_definition = cast_definitions[key.to_sym]
casted_value = type_cast_value(value, type_definition)
hash[key.to_s] = casted_value
instance_variable_set("@#{key}", casted_value)
end
super() if defined?(super)
end
define_method :inspect do
attrs = attribute_names.map do |name|
"#{name}=#{instance_variable_get("@#{name}").inspect}"
end.join(" ")
"#<#{self.class.superclass.name} #{attrs}>"
end
define_method(:attribute_names) { attribute_names }
end
end
|
.with(params = {}) ⇒ Object
12
13
14
15
16
17
18
19
20
|
# File 'lib/kweerie/base_object.rb', line 12
def with(params = {})
results = super
return [] if results.empty?
result_class = generate(results.first.keys)
results.map { |row| result_class.new(row) }
end
|