Class: Pigeon::Channel
- Inherits:
-
Object
show all
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Includes:
- ActiveModel::Conversion, ActiveModel::Validations, NestedScopes
- Defined in:
- app/models/pigeon/channel.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#find_attr_recursive, #find_attr_ref_recursive, #fold_scoped_name
Constructor Details
#initialize(attrs = {}, persisted = false) ⇒ Channel
Returns a new instance of Channel.
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'app/models/pigeon/channel.rb', line 86
def initialize(attrs = {}, persisted = false)
@errors = ChannelErrors.new(self)
@attributes = {}.with_indifferent_access
@persisted = persisted
@destroyed = false
attrs = attrs.dup
@schema = attrs.delete(:schema) || self.class.find_schema(attrs[:kind])
attrs[:kind] ||= @schema.kind unless @schema.nil?
load_default_values
load_schema_defaults
load(attrs)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_symbol, *arguments) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'app/models/pigeon/channel.rb', line 125
def method_missing(method_symbol, *arguments)
method_name = method_symbol.to_s
if method_name =~ /(=|\?)$/
case $1
when "="
attributes[$`] = arguments.first
when "?"
attributes[$`]
end
else
return attributes[method_name] if attributes.include?(method_name)
return nil if known_attributes.include?(method_name)
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
75
76
77
|
# File 'app/models/pigeon/channel.rb', line 75
def attributes
@attributes
end
|
#schema ⇒ Object
Returns the value of attribute schema.
76
77
78
|
# File 'app/models/pigeon/channel.rb', line 76
def schema
@schema
end
|
Class Method Details
.all ⇒ Object
64
|
# File 'app/models/pigeon/channel.rb', line 64
def all() find(:all) end
|
.channel_accessor(*names) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
|
# File 'app/models/pigeon/channel.rb', line 32
def channel_accessor(*names)
options = names.
names.each do |name|
reader, line = "def #{name}; attributes['#{name}']; end", __LINE__
writer, line = "def #{name}=(value); attributes['#{name}'] = value; end", __LINE__
class_eval reader, __FILE__, line unless options[:instance_reader] == false
class_eval writer, __FILE__, line unless options[:instance_writer] == false
end
end
|
.find(*arguments) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'app/models/pigeon/channel.rb', line 48
def find(*arguments)
scope = arguments.slice!(0)
options = arguments.slice!(0) || {}
case scope
when :all
list.map { |id| find_single(id) }
when :first
find_single(list.first)
when :last
find_single(list.first)
else
find_single(scope)
end
end
|
.find_schema(kind) ⇒ Object
28
29
30
|
# File 'app/models/pigeon/channel.rb', line 28
def find_schema(kind)
schemas.find { |s| s.kind == kind }
end
|
.find_type(type) ⇒ Object
20
21
22
23
24
25
26
|
# File 'app/models/pigeon/channel.rb', line 20
def find_type(type)
begin
"Pigeon::#{type.to_s.capitalize}Channel".constantize
rescue
nil
end
end
|
.first ⇒ Object
65
|
# File 'app/models/pigeon/channel.rb', line 65
def first() find(:first) end
|
.i18n_scope ⇒ Object
10
11
12
|
# File 'app/models/pigeon/channel.rb', line 10
def i18n_scope
:pigeon
end
|
.last ⇒ Object
66
|
# File 'app/models/pigeon/channel.rb', line 66
def last() find(:last) end
|
.list ⇒ Object
44
45
46
|
# File 'app/models/pigeon/channel.rb', line 44
def list
raise NotImplementedError
end
|
.schemas ⇒ Object
16
17
18
|
# File 'app/models/pigeon/channel.rb', line 16
def schemas
[]
end
|
.type ⇒ Object
14
|
# File 'app/models/pigeon/channel.rb', line 14
def type() nil end
|
Instance Method Details
#[](key) ⇒ Object
151
152
153
|
# File 'app/models/pigeon/channel.rb', line 151
def [](key)
attributes[key]
end
|
#[]=(key, value) ⇒ Object
155
156
157
|
# File 'app/models/pigeon/channel.rb', line 155
def []=(key, value)
attributes[key] = value
end
|
#assign_attributes(new_attributes) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
|
# File 'app/models/pigeon/channel.rb', line 168
def assign_attributes(new_attributes)
return if new_attributes.blank?
new_attributes = new_attributes.stringify_keys
new_attributes.each do |key, value|
if value.is_a?(Hash)
write_attribute(key, read_attribute(key).merge(value))
else
write_attribute(key, value)
end
end
end
|
#destroy ⇒ Object
199
200
201
|
# File 'app/models/pigeon/channel.rb', line 199
def destroy
raise NotImplementedError
end
|
#destroyed? ⇒ Boolean
114
115
116
|
# File 'app/models/pigeon/channel.rb', line 114
def destroyed?
@destroyed
end
|
#generate_name ⇒ Object
143
144
145
|
# File 'app/models/pigeon/channel.rb', line 143
def generate_name
"#{kind || 'channel'}-#{Time.now.strftime('%Y%m%d%H%M%S%3N')}"
end
|
#generate_name! ⇒ Object
147
148
149
|
# File 'app/models/pigeon/channel.rb', line 147
def generate_name!
self.name = generate_name
end
|
#human_attribute_name(attr_name, options = {}) ⇒ Object
186
187
188
189
|
# File 'app/models/pigeon/channel.rb', line 186
def human_attribute_name(attr_name, options = {})
(!schema.nil? && schema.find_attribute(attr_name).try(:humanized_name)) ||
self.class.human_attribute_name(attr_name, options)
end
|
#known_attributes ⇒ Object
121
122
123
|
# File 'app/models/pigeon/channel.rb', line 121
def known_attributes
schema.try(:known_attributes) || []
end
|
#new_record? ⇒ Boolean
110
111
112
|
# File 'app/models/pigeon/channel.rb', line 110
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
106
107
108
|
# File 'app/models/pigeon/channel.rb', line 106
def persisted?
@persisted
end
|
#read_attribute(attr_name) ⇒ Object
159
160
161
|
# File 'app/models/pigeon/channel.rb', line 159
def read_attribute(attr_name)
find_attr_recursive(attr_name, attributes)
end
|
#read_attribute_for_validation(key) ⇒ Object
102
103
104
|
# File 'app/models/pigeon/channel.rb', line 102
def read_attribute_for_validation(key)
attributes[key]
end
|
#save ⇒ Object
191
192
193
|
# File 'app/models/pigeon/channel.rb', line 191
def save
raise NotImplementedError
end
|
#save! ⇒ Object
195
196
197
|
# File 'app/models/pigeon/channel.rb', line 195
def save!
save || raise(ChannelInvalid.new(self))
end
|
#type ⇒ Object
82
83
84
|
# File 'app/models/pigeon/channel.rb', line 82
def type
self.class.type
end
|
#write_attribute(attr_name, value) ⇒ Object
163
164
165
166
|
# File 'app/models/pigeon/channel.rb', line 163
def write_attribute(attr_name, value)
hash, key = find_attr_ref_recursive(attr_name, attributes)
hash && hash[key] = value
end
|