Class: Aric::Resource::Base

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

Direct Known Subclasses

Playlist, Track

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Base

Returns a new instance of Base.



38
39
40
41
42
43
44
45
46
47
# File 'lib/aric/resource/base.rb', line 38

def initialize(resource)
  properties = resource.delete('properties') || {}
  @resource = Hashie::Mash.new(resource.merge(properties))

  @resource.select { |_, v| [TrueClass, FalseClass].include?(v.class) }.each do |k, v|
    self.class.class_eval do
      define_method("#{k}?") { v }
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/aric/resource/base.rb', line 49

def method_missing(method_name, *args, &block)
  if @resource.respond_to?(method_name)
    @resource.send(method_name, *args, &block)
  else
    super
  end
end

Class Method Details

.allObject



19
20
21
# File 'lib/aric/resource/base.rb', line 19

def all
  find_by(:all)
end

.find_by(cond) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
# File 'lib/aric/resource/base.rb', line 8

def find_by(cond)
  raise ArgumentError if cond.nil?
  if cond.is_a?(Hash)
    find_with_condition(cond)
  elsif cond == :all
    find_all
  else
    raise ArgumentError
  end
end