Class: MtgApi::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mtg_api/config.rb

Overview

the configured properties of an entity

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clazz) ⇒ Config

store the class name and initialize empty lists



10
11
12
13
14
15
# File 'lib/mtg_api/config.rb', line 10

def initialize(clazz)
  self.name = clazz.name
  self.attributes = []
  self.properties = []
  self.setters = {}
end

Instance Attribute Details

#attributesObject

the stored properties of this config



7
8
9
# File 'lib/mtg_api/config.rb', line 7

def attributes
  @attributes
end

#nameObject

the stored properties of this config



7
8
9
# File 'lib/mtg_api/config.rb', line 7

def name
  @name
end

#propertiesObject

the stored properties of this config



7
8
9
# File 'lib/mtg_api/config.rb', line 7

def properties
  @properties
end

#settersObject

the stored properties of this config



7
8
9
# File 'lib/mtg_api/config.rb', line 7

def setters
  @setters
end

Instance Method Details

#accessorsObject

the list of attributes to build attr_accessors for



18
19
20
# File 'lib/mtg_api/config.rb', line 18

def accessors
  attributes + properties - setters.keys
end

#attribute(*attributes) ⇒ Object

add to the attributes list (queryable)



23
24
25
# File 'lib/mtg_api/config.rb', line 23

def attribute(*attributes)
  self.attributes += attributes
end

#endpointObject

the endpoint to send to the api for the entity



28
29
30
# File 'lib/mtg_api/config.rb', line 28

def endpoint
  @endpoint ||= '/' + response_key
end

#full_configObject

the full list of attributes set



33
34
35
# File 'lib/mtg_api/config.rb', line 33

def full_config
  (attributes + properties + setters.keys).uniq
end

#property(*properties) ⇒ Object

add to the properties list (unqueryable)



38
39
40
# File 'lib/mtg_api/config.rb', line 38

def property(*properties)
  self.properties += properties
end

#response_keyObject

the key in the response for the api



43
44
45
# File 'lib/mtg_api/config.rb', line 43

def response_key
  @response_key ||= name.downcase.split('::').last + 's'
end

#setter(attribute, clazz = nil, &block) ⇒ Object

build a setter that can map the return value from the api



48
49
50
51
52
53
# File 'lib/mtg_api/config.rb', line 48

def setter(attribute, clazz = nil, &block)
  setters[attribute] = proc do |value|
    value = clazz.nil? ? block.call(value) : clazz.new(value)
    instance_variable_set(:"@#{attribute}", value)
  end
end