Module: Kippt::Resource::ClassMethods

Defined in:
lib/kippt/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



16
17
18
# File 'lib/kippt/resource.rb', line 16

def attribute_names
  @attribute_names
end

#writable_attribute_namesObject (readonly)

Returns the value of attribute writable_attribute_names.



16
17
18
# File 'lib/kippt/resource.rb', line 16

def writable_attribute_names
  @writable_attribute_names
end

Instance Method Details

#attributes(*attribs) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/kippt/resource.rb', line 22

def attributes(*attribs)
  @attribute_names ||= []
  hashes, other = attribs.partition {|attrib| attrib.is_a? Hash }

  attribute_names = convert_to_symbols(other)
  def_delegators :attributes, *attribute_names
  @attribute_names += attribute_names

  mappings = hashes.reduce({}, :update)
  mappings.each do |attrib, object_class|
    if object_class.to_s == "Time"
      define_method(attrib) do
        Time.at(attributes.send(attrib))
      end
    else
      reified_class = _get_class(object_class)
      define_method(attrib) do
        value = attributes.send(attrib)
        reified_class.new(value) unless value.nil?
      end
    end
  end
  @attribute_names += convert_to_symbols(mappings.keys)
end

#boolean_attributes(*attribs) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kippt/resource.rb', line 47

def boolean_attributes(*attribs)
  @attribute_names ||= []
  attribute_names = convert_to_symbols(attribs)
  def_delegators :attributes, *attribute_names
  @attribute_names += attribute_names

  attribs.each do |attribute_name|
    if result = attribute_name.to_s.match(/\Ais\_(.*)/)
      define_method "#{result[1]}?" do
        send(attribute_name) if respond_to?(attribute_name)
      end
    end
  end
end

#embedded_attribute_namesObject



18
19
20
# File 'lib/kippt/resource.rb', line 18

def embedded_attribute_names
  @embedded_attribute_names || []
end

#embedded_attributes(*attribs) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/kippt/resource.rb', line 68

def embedded_attributes(*attribs)
  mappings = attribs.reduce({}, :update)
  @embedded_attribute_names = convert_to_symbols(mappings.keys)
  @embedded_attribute_names.freeze

  mappings.each do |attrib, attribute_class|
    reified_class = _get_class(attribute_class)
    define_method(attrib) do
      value = attributes.send(attrib)
      if value.is_a? String
        client.resource_from_url(reified_class, value)
      elsif value.is_a? reified_class
        value
      else
        reified_class.new(value, client)
      end
    end
  end
end

#writable_attributes(*attribs) ⇒ Object



62
63
64
65
66
# File 'lib/kippt/resource.rb', line 62

def writable_attributes(*attribs)
  @writable_attribute_names = convert_to_symbols(attribs)
  @writable_attribute_names.freeze
  def_delegators :attributes, *(@writable_attribute_names.map {|attrib| attrib.to_s + "=" }).dup
end