Class: Unimatrix::Activist::Base
- Inherits:
-
Object
- Object
- Unimatrix::Activist::Base
show all
- Defined in:
- lib/unimatrix/activist/resources/base.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}, associations = {}) {|_self| ... } ⇒ Base
Returns a new instance of Base.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/unimatrix/activist/resources/base.rb', line 54
def initialize( attributes={}, associations={} )
self.type_name = self.class.name.gsub( /Unimatrix::Activist::/, '' ).underscore
attributes.each do | key, value |
if !respond_to?( "#{ key }=" )
field( key )
end
send( "#{ key }=", value )
end
associations.each do | key, value |
self.instance_variable_set( "@_#{ key }", value )
end
yield self if block_given?
end
|
Class Method Details
.field(name, options = {}) ⇒ Object
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/unimatrix/activist/resources/base.rb', line 11
def field( name, options = {} )
self.fields[ name.to_sym ] = options.merge( name: name )
class_eval(
"def #{ name }(); " +
"@#{ name }.is_a?( FalseClass ) ? @#{ name } : (" +
"@#{ name } || " +
( options[ :default ].nil? ?
"nil" :
( options[ :default ].is_a?( String ) ?
"'#{ options[ :default ] }'" :
"#{ options[ :default ] }" ) ) + ");" +
"end;" +
" " +
"attr_writer :#{ name };",
__FILE__,
__LINE__
)
end
|
.has_many(name, options = {}) ⇒ Object
39
40
41
42
43
44
|
# File 'lib/unimatrix/activist/resources/base.rb', line 39
def has_many( name, options = {} )
define_method name do
self.instance_variable_get( "@_#{ name }" ) ||
options[ :default ] || []
end
end
|
.has_one(name, options = {}) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/unimatrix/activist/resources/base.rb', line 32
def has_one( name, options = {} )
define_method name do
associations = self.instance_variable_get( "@_#{ name.to_s.pluralize }" )
associations.present? ? associations.first : options[ :default ]
end
end
|
.inherited(subclass) ⇒ Object
7
8
9
|
# File 'lib/unimatrix/activist/resources/base.rb', line 7
def inherited( subclass )
subclass.fields = {}.merge( self.fields )
end
|
Instance Method Details
#field(name, options = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/unimatrix/activist/resources/base.rb', line 72
def field( name, options = {} )
self.fields[ name.to_sym ] = options.merge( name: name )
class_eval(
"def #{ name }(); " +
"@#{ name }.is_a?( FalseClass ) ? @#{ name } : (" +
"@#{ name } || " +
( options[ :default ].nil? ?
"nil" :
( options[ :default ].is_a?( String ) ?
"'#{ options[ :default ] }'" :
"#{ options[ :default ] }" ) ) + ");" +
"end;" +
" " +
"attr_writer :#{ name };",
__FILE__,
__LINE__
)
end
|