Module: Pgai::Resources::Attributes

Included in:
Local::BaseRecord, Remote::BaseRecord
Defined in:
lib/pgai/resources/attributes.rb

Defined Under Namespace

Modules: ClassMethods Classes: Attribute

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



8
9
10
11
# File 'lib/pgai/resources/attributes.rb', line 8

def self.included(base)
  base.extend ClassMethods
  attr_reader :attributes
end

Instance Method Details

#assign_attribute(attribute, user_value) ⇒ Object



89
90
91
92
93
# File 'lib/pgai/resources/attributes.rb', line 89

def assign_attribute(attribute, user_value)
  value = attribute.cast_or_default(user_value)

  public_send(:"#{attribute.name}=", value)
end

#assign_attributes(user_attributes) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/pgai/resources/attributes.rb', line 95

def assign_attributes(user_attributes)
  user_attributes.each do |key, value|
    attribute = self.class.attributes.find { |attr| attr.name == key }
    raise "shit" unless attribute

    assign_attribute(attribute, value)
  end
end

#has_attribute?(name) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/pgai/resources/attributes.rb', line 104

def has_attribute?(name)
  respond_to?(name) && respond_to?(:"#{name}=")
end

#initialize(user_attributes = {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/pgai/resources/attributes.rb', line 81

def initialize(user_attributes = {})
  @attributes = {}

  self.class.attributes.each do |attribute|
    assign_attribute(attribute, user_attributes[attribute.name])
  end
end