Module: Supplejack::Record
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/supplejack/record.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
Instance Method Summary collapse
- #format ⇒ Object
- #id ⇒ Object
- #initialize(attributes = {}) ⇒ Object
-
#metadata ⇒ Object
Returns a array of hashes containing all the record attributes and the schema each attribute belongs to.
-
#method_missing(symbol, *args, &block) ⇒ Object
def build_authorities(name) @attributes ||= [] authorities = @attributes.find_all {|authority| authority == name } authorities.map {|attributes| Supplejack::Authority.new(attributes) } end.
- #persisted? ⇒ Boolean
- #title ⇒ Object
- #to_param ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args, &block) ⇒ Object
def build_authorities(name)
@attributes[:authorities] ||= []
= @attributes[:authorities].find_all {|| ["name"] == name }
.map {|attributes| Supplejack::Authority.new(attributes) }
end
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/supplejack/record.rb', line 108 def method_missing(symbol, *args, &block) if symbol.to_s.match(/(.+)_authorities/) return ("#{$1}_authority") end if symbol.to_s.match(/(.+)_terms/) return ("#{$1}_term") end if [:series_parent, :child_series, :collection_parent, :collection_root, :collection_mid].include?(symbol) return (symbol.to_s) end raise NoMethodError, "undefined method '#{symbol.to_s}' for Supplejack::Record:Module" unless @attributes.has_key?(symbol) @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
#format ⇒ Object
87 88 89 90 |
# File 'lib/supplejack/record.rb', line 87 def format raise NoMethodError, "undefined method 'format' for Supplejack::Record:Module" unless @attributes.has_key?(:format) @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 |
#metadata ⇒ Object
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 85 |
# File 'lib/supplejack/record.rb', line 59 def = [] 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 #Testing if boolean 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.camelcase(:lower) if schema == :dcterms field = field.to_s.sub(/#{schema}_/, '') values.each do |value| << {:name => field, :schema => schema.to_s, :value => value } end end end end end |
#persisted? ⇒ Boolean
98 99 100 |
# File 'lib/supplejack/record.rb', line 98 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 |