Module: Supplejack::Record
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/supplejack/record.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
103
104
105
106
107
108
|
# File 'lib/supplejack/record.rb', line 103
def method_missing(symbol, *args, &block)
unless @attributes.has_key?(symbol)
raise NoMethodError, "undefined method '#{symbol.to_s}' for Supplejack::Record:Module"
end
@attributes[symbol]
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
14
15
16
|
# File 'lib/supplejack/record.rb', line 14
def attributes
@attributes
end
|
Instance Method Details
86
87
88
89
90
91
|
# File 'lib/supplejack/record.rb', line 86
def format
unless @attributes.has_key?(:format)
raise NoMethodError, "undefined method 'format' for Supplejack::Record:Module"
end
@attributes[:format]
end
|
#id ⇒ Object
39
40
41
42
|
# File 'lib/supplejack/record.rb', line 39
def id
id = @attributes[:id] || @attributes[:record_id]
id.to_i
end
|
#initialize(attributes = {}) ⇒ Object
32
33
34
35
36
37
|
# File 'lib/supplejack/record.rb', line 32
def initialize(attributes={})
if attributes.is_a?(String)
attributes = JSON.parse(attributes) rescue {}
end
@attributes = attributes.symbolize_keys rescue {}
end
|
Returns a array of hashes containing all the record attributes and the schema each attribute belongs to. To set what fields belong to each schema there is a config option to set supplejack_fields and admin_fields
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
|
# File 'lib/supplejack/record.rb', line 59
def metadata
metadata = []
Supplejack.send('special_fields').each do |schema, fields|
fields[:fields].each do |field|
if @attributes.has_key?(field)
values = @attributes[field]
values ||= [] unless !!values == values values = [values] unless values.is_a?(Array)
case fields[:format]
when "uppercase" then field = field.to_s.upcase
when "lowercase" then field = field.to_s.downcase
when "camelcase" then field = field.to_s.camelcase
end
field = field.to_s.sub(/#{schema}_/, '')
values.each do |value|
metadata << {:name => field, :schema => schema.to_s, :value => value }
end
end
end
end
metadata
end
|
#persisted? ⇒ Boolean
99
100
101
|
# File 'lib/supplejack/record.rb', line 99
def persisted?
true
end
|
#title ⇒ Object
48
49
50
|
# File 'lib/supplejack/record.rb', line 48
def title
@attributes[:title].present? ? @attributes[:title] : "Untitled"
end
|
#to_param ⇒ Object
44
45
46
|
# File 'lib/supplejack/record.rb', line 44
def to_param
self.id
end
|