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

Raises:

  • (NoMethodError)


95
96
97
98
# File 'lib/supplejack/record.rb', line 95

def method_missing(symbol, *args, &block)
  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)


70
71
72
73
# File 'lib/supplejack/record.rb', line 70

def format
  raise NoMethodError, "undefined method 'format' for Supplejack::Record:Module" unless @attributes.has_key?(:format)
  @attributes[:format]
end

#idObject



29
30
31
32
# File 'lib/supplejack/record.rb', line 29

def id
  id = @attributes[:id] || @attributes[:record_id]
  id.to_i
end

#initialize(attributes = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/supplejack/record.rb', line 22

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" }, ...]


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/supplejack/record.rb', line 49

def 
   = []

  ['supplejack', 'admin'].each do |schema|
    Supplejack.send("#{schema}_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)
        field = field.to_s.camelcase(:lower) if schema == "dcterms"
        field = field.to_s.sub(/#{schema}_/, '')
        values.each do |value|
           << {:name => field, :schema => schema, :value => value }
        end
      end
    end
  end

  
end

#persisted?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/supplejack/record.rb', line 91

def persisted?
  true
end

#titleObject



38
39
40
# File 'lib/supplejack/record.rb', line 38

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

#to_paramObject



34
35
36
# File 'lib/supplejack/record.rb', line 34

def to_param
  self.id
end