Module: Videojuicer::Resource::PropertyRegistry

Included in:
Videojuicer::Resource
Defined in:
lib/videojuicer/resource/property_registry.rb

Defined Under Namespace

Modules: SingletonMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/videojuicer/resource/property_registry.rb', line 17

def self.included(base)        
  # Class-level inheritable reader
  base.extend(SingletonMethods)
  base.class_eval do
    @attributes = {}
    class << self
      attr_accessor :attributes
    end
  end
  base.property :id, Integer
end

.inherited(subclass) ⇒ Object

Allow subclasses of each resource to get the attributes accessor



30
31
32
33
# File 'lib/videojuicer/resource/property_registry.rb', line 30

def self.inherited(subclass)
  v = "@attributes"
  subclass.instance_variable_set(v, instance_variable_get(v))
end

Instance Method Details

#attr_clean!(key) ⇒ Object

Mark the specified attribute as no longer being dirty.



105
106
107
108
# File 'lib/videojuicer/resource/property_registry.rb', line 105

def attr_clean!(key)
  @dirty_attribute_keys ||= []
  @dirty_attribute_keys.delete_if {|k| k == key.to_sym }
end

#attr_dirty!(key) ⇒ Object

Mark the specified attribute as dirty.



98
99
100
101
102
# File 'lib/videojuicer/resource/property_registry.rb', line 98

def attr_dirty!(key)
  @dirty_attribute_keys ||= []
  @dirty_attribute_keys << key.to_sym
  @dirty_attribute_keys.uniq!
end

#attr_dirty?(key) ⇒ Boolean

Returns true if the specified attribute is currently dirty

Returns:



92
93
94
95
# File 'lib/videojuicer/resource/property_registry.rb', line 92

def attr_dirty?(key)
  @dirty_attribute_keys ||= []
  @dirty_attribute_keys.include?(key.to_sym)
end

#attr_get(key) ⇒ Object



110
111
112
113
# File 'lib/videojuicer/resource/property_registry.rb', line 110

def attr_get(key)
  key = key.to_sym
  attributes[key]
end

#attr_set(key, value) ⇒ Object



115
116
117
118
119
# File 'lib/videojuicer/resource/property_registry.rb', line 115

def attr_set(key, value)
  key = key.to_sym
  attr_dirty!(key)
  attributes[key] = coerce_value(key, value)
end

#attributesObject



40
41
42
43
# File 'lib/videojuicer/resource/property_registry.rb', line 40

def attributes
  @attributes ||= {}
  @attributes
end

#attributes=(arg) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
# File 'lib/videojuicer/resource/property_registry.rb', line 45

def attributes=(arg)
  raise ArgumentError, "Attributes must be set as a Hash" unless arg.is_a?(Hash)
  arg.each do |key, value|
    #set any attributes, ignoring all those that are invalid  
    self.send("#{key}=", value) rescue invalid_attributes[key] = value
  end
end

#clean_dirty_attributes!Object

Clears the array of dirty attribute keys



81
82
83
# File 'lib/videojuicer/resource/property_registry.rb', line 81

def clean_dirty_attributes!
  @dirty_attribute_keys = []
end

#coerce_value(key, value) ⇒ Object

Takes what is normally a string and coerces it into the correct object type for the attribute’s actual type.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/videojuicer/resource/property_registry.rb', line 123

def coerce_value(key, value)
  return value unless value
  klass = self.class.attributes[key][:class]
  
  if value.is_a?(String) and !value.empty?
    # In-built types
    if klass.kind_of?(Videojuicer::Resource::Types::Base)
      return klass.new(value).dump
    end
    
    # Dates
    if klass.respond_to?(:parse)
      return klass.parse(value) rescue raise "Invalid date: #{value.inspect}"
    end
  elsif value.is_a? Hash and value.any?
    if klass == DateTime
      if value.is_a?(Hash)
        year   = value[:year]
        month  = value[:month]
        day    = value[:day]
        hour   = value[:hour] or "00"
        minute = value[:minute] or "00"
        value = klass.parse("#{year}-#{month}-#{day}T#{hour}:#{minute}:00+00:00")
      else
        raise ArgumentError, "Please supply a DateTime, Hash keyed w/ [:day, :month, :year, :hour, :minute] or a String that can be coerced into a date"
      end
    end
  end
  return value
end

#default_attributesObject



62
63
64
65
66
67
68
# File 'lib/videojuicer/resource/property_registry.rb', line 62

def default_attributes
  d = {}
  self.class.attributes.each do |key, props|
    d[key] = props[:default] || nil
  end
  return d
end

#dirty_attribute_keysObject

Returns an array of keys for attributes that are currently dirty



86
87
88
89
# File 'lib/videojuicer/resource/property_registry.rb', line 86

def dirty_attribute_keys
  @dirty_attribute_keys ||= []
  @dirty_attribute_keys
end

#dirty_attributesObject

Returns the hash of currently-dirty attributes complete with values



71
72
73
74
75
76
77
78
# File 'lib/videojuicer/resource/property_registry.rb', line 71

def dirty_attributes
  o = {}
  @dirty_attribute_keys ||= []
  @dirty_attribute_keys.each do |key|
    o[key] = attr_get(key)
  end
  o
end

#initialize(attrs = {}) ⇒ Object



35
36
37
38
# File 'lib/videojuicer/resource/property_registry.rb', line 35

def initialize(attrs={})
  set_default_attributes
  self.attributes = attrs
end

#invalid_attributesObject



165
166
167
# File 'lib/videojuicer/resource/property_registry.rb', line 165

def invalid_attributes
  @invalid_attributes ||= {}
end

#returnable_attributesObject

Returns a hash of the attributes for this object, minus the private attributes that should not be included in the response.



156
157
158
159
160
161
162
163
# File 'lib/videojuicer/resource/property_registry.rb', line 156

def returnable_attributes
  attrs = dirty_attributes.dup
  self.class.attributes.select {|name, props| props[:writer] != :public}.each do |name, props|
    attrs.delete name
  end
  attrs.delete(:id)
  attrs
end

#set_default_attributesObject

Sets the attributes to their default values, marking only those values with defaults as being dirty.



54
55
56
57
58
59
60
# File 'lib/videojuicer/resource/property_registry.rb', line 54

def set_default_attributes
  self.attributes = default_attributes
  self.attributes.each do |key, value|
    # Scrub the attributes if there's no value
    attr_clean!(key) unless value
  end
end