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

def build_authorities(name)

@attributes[:authorities] ||= []
authorities = @attributes[:authorities].find_all {|authority| authority["name"] == name }
authorities.map {|attributes| Supplejack::Authority.new(attributes) }

end

Raises:

  • (NoMethodError)


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 build_authorities("#{$1}_authority")
  end

  if symbol.to_s.match(/(.+)_terms/)
    return build_authorities("#{$1}_term")
  end

  if [:series_parent, :child_series, :collection_parent, :collection_root, :collection_mid].include?(symbol)
    return build_authorities(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

#attributesObject

Returns the value of attribute attributes.



14
15
16
# File 'lib/supplejack/record.rb', line 14

def attributes
  @attributes
end

Instance Method Details

#formatObject

Raises:

  • (NoMethodError)


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

#idObject



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

#metadataObject

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

Examples:

record. => [{:name => "location", :schema => "supplejack", :value => "Wellington" }, ...]


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

Returns:

  • (Boolean)


98
99
100
# File 'lib/supplejack/record.rb', line 98

def persisted?
  true
end

#titleObject



48
49
50
# File 'lib/supplejack/record.rb', line 48

def title
  @attributes[:title].present? ? @attributes[:title] : "Untitled"
end

#to_paramObject



44
45
46
# File 'lib/supplejack/record.rb', line 44

def to_param
  self.id
end