Class: Serel::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/serel/base.rb

Defined Under Namespace

Classes: Boolean

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/serel/base.rb', line 9

def initialize(data)
  @data = {}
  attributes.each { |k,v| @data[k] = data[k.to_s] }
  if associations
    associations.each do |k,v|
      if data[k.to_s]
        @data[k] = Serel.const_get(v.to_s.classify).new(data[k.to_s])
      end
    end
  end
end

Class Method Details

.associations(options = {}) ⇒ Object

Internal: Defines the associations for a subclass of Serel::Base

options - The Hash options used to define associations

{ :name => :type }
:name - The Symbol name that this association will be found as in the data
:type - The Symbol type that this association should be parsed as

Returns nothing.



81
82
83
84
85
86
87
88
89
# File 'lib/serel/base.rb', line 81

def self.associations(options = {})
  self.associations = options
  options.each do |meth, type|
    unless self.respond_to?(meth)
      define_method(meth) { self[meth.to_sym] }
    end
    define_method("#{meth}=") { |val| self[meth.to_sym] = val }
  end
end

.attribute(name, klass) ⇒ Object

Defines an attribute on a subclass of Serel::Base

Parameters:

  • name (Symbol)

    The name of the attribute

  • klass (Class)

    The type of the returned attributed

Returns:

  • Nothing of value



66
67
68
69
70
71
# File 'lib/serel/base.rb', line 66

def self.attribute(name, klass)
  self.attributes = {} unless self.attributes
  self.attributes[name] = klass
  define_method(name) { self[name.to_sym] }
  define_method("#{name}=") { |val| self[name.to_sym] = val }
end

.config(site, api_key = nil) ⇒ Object

Public: Sets the global configuration values.

site - The API site parameter that represents the site you wish to query. api_key - Your API key.

Returns nothing.



37
38
39
40
41
42
43
44
45
# File 'lib/serel/base.rb', line 37

def self.config(site, api_key = nil)
  self.site = site.to_sym
  self.api_key = api_key
  self.logger = Logger.new(STDOUT)
  self.logger.formatter = proc { |severity, datetime, progname, msg|
    %([#{severity}][#{datetime.strftime("%Y-%m-%d %H:%M:%S")}] #{msg}\n)
  }
  nil # Return nothing, rather than that proc
end

.finder_methods(*splat) ⇒ Object

Public: Sets the finder methods available to this type

*splat - The Array of methods this class accepts. Also accepts :every,

which indicates that all finder methods are valid

Returns nothing of value



108
109
110
# File 'lib/serel/base.rb', line 108

def self.finder_methods(*splat)
  self.finder_methods = splat
end

.method_missing(sym, *attrs, &block) ⇒ Object



182
183
184
185
186
187
# File 'lib/serel/base.rb', line 182

def self.method_missing(sym, *attrs, &block)
  # TODO: see if the new_relation responds to the method being called
  #       requires a rewrite of how method_missing works on the
  #       relation side.
  new_relation.send(sym, *attrs, &block)
end

.network_wideObject

Denotes that a class does not need the site parameter



113
114
115
# File 'lib/serel/base.rb', line 113

def self.network_wide
  self.network = true
end

.new_relation(klass = nil, qty = :singular) ⇒ Object

Public: Creates a new relation scoped to the class

klass - the name of the class to scope the relation to

Returns an instance of Serel::Relation



96
97
98
99
# File 'lib/serel/base.rb', line 96

def self.new_relation(klass = nil, qty = :singular)
  klass = name.split('::').last.underscore unless klass
  Serel::Relation.new(klass.to_s, qty)
end

.request(path, type = nil) ⇒ Object



177
# File 'lib/serel/base.rb', line 177

def self.request(path, type = nil); new_relation.request(path, type); end

.respond_to?(method_sym, include_private = true) ⇒ Boolean

Public: Provides an interface to show which methods this class responds to

method_sym - The Symbol representation of the method you are interested in include_private - Whether to include private methods. We ignore this, but

it is part of how the core library handles respond_to

Returns Boolean indicating whether the class responds to it or not

Returns:



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/serel/base.rb', line 131

def self.respond_to?(method_sym, include_private = true)
  if self.all_finder_methods.include?(method_sym.to_s)
    if (self.finder_methods.include?(:every)) || (self.finder_methods.include?(method_sym))
      true
    else
      false
    end
  else
    super(method_sym, include_private)
  end
end

.with_ids(*ids) ⇒ Object

Create an ‘shallow’ instance with multiple IDs. Used for vectorized requests



118
119
120
121
122
# File 'lib/serel/base.rb', line 118

def self.with_ids(*ids)
  idstr = ids.join(";")
  n = name.split('::').last.underscore
  new({"#{n}_id".to_s => idstr})
end

Instance Method Details

#[](attr) ⇒ Object

Internal: Provides access to the internal data Rather than store data using attr_writers (problems) we use a hash.



23
24
25
# File 'lib/serel/base.rb', line 23

def [](attr)
  format_attribute(attr)
end

#[]=(attr, value) ⇒ Object



27
28
29
# File 'lib/serel/base.rb', line 27

def []=(attr, value)
  @data[attr] = value
end

#allObject



143
144
145
146
147
148
149
# File 'lib/serel/base.rb', line 143

def all
  if self.respond_to?(:all)
    new_relation.all
  else
    raise NoMethodError
  end
end

#find(id) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/serel/base.rb', line 151

def find(id)
  if self.respond_to?(:find)
    new_relation.find(id)
  else
    raise NoMethodError
  end
end

#getObject



159
160
161
162
163
164
165
# File 'lib/serel/base.rb', line 159

def get
  if self.respond_to?(:get)
    new_relation(name.split('::').last.underscore, :plural).get
  else
    raise NoMethodError
  end
end

#inspectObject Also known as: to_s

Public: Provides a nice and quick way to inspec the properties of a

Serel instance inheriting from Serel::Base

Returns a String representation of the class



51
52
53
54
55
56
57
58
59
# File 'lib/serel/base.rb', line 51

def inspect
  attribute_collector = {}
  self.class.attributes.each { |attr, type| attribute_collector[attr] = self.send(attr) }
  inspected_attributes = attribute_collector.select { |k,v| v != nil }.collect { |k,v| "@#{k}=#{v}" }.join(", ")
  association_collector = {}
  self.class.associations.each { |name, type| association_collector[name] = self[name] }
  inspected_associations = association_collector.select { |k, v| v != nil }.collect { |k, v| "@#{k}=#<#{v.class}:#{v.object_id}>" }.join(", ")
  "#<#{self.class}:#{self.object_id} #{inspected_attributes} #{inspected_associations}>"
end

#methObject

Pass these methods direct to a new Relation, which should not be singular. >: TODO: clean these up



169
170
171
172
173
174
175
# File 'lib/serel/base.rb', line 169

%w(access_token filter fromdate inname intitle min max nottagged order page pagesize since sort tagged title todate url).each do |meth|
  class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def self.#{meth}(val)
      new_relation(name.split('::').last.underscore, :plural).#{meth}(val)
    end
  RUBY
end

#networkObject



176
# File 'lib/serel/base.rb', line 176

def network; self.class.new_relation.network; end

#type(klass, qty = :plural) ⇒ Object



178
179
180
# File 'lib/serel/base.rb', line 178

def type(klass, qty = :plural)
  self.class.new_relation(klass, qty)
end