Class: Effective::ModelReader

Inherits:
Object
  • Object
show all
Defined in:
app/models/effective/model_reader.rb

Constant Summary collapse

DATATYPES =
[:binary, :boolean, :date, :datetime, :decimal, :float, :hstore, :inet, :integer, :string, :text, :permitted_param]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ModelReader

Returns a new instance of ModelReader.



7
8
9
# File 'app/models/effective/model_reader.rb', line 7

def initialize(&block)
  @attributes = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/effective/model_reader.rb', line 15

def method_missing(m, *args, &block)
  if m == :timestamps
    attributes[:created_at] = [:datetime]
    attributes[:updated_at] = [:datetime]
    return
  end

  # Not really an attribute, just a permitted param.
  args.unshift(:permitted_param) if args.first.kind_of?(Hash) && args.first.key?(:permitted)

  unless DATATYPES.include?(args.first)
    raise "expected first argument to be a datatype. Try name :string"
  end

  attributes[m] = args
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'app/models/effective/model_reader.rb', line 5

def attributes
  @attributes
end

Instance Method Details

#read(&block) ⇒ Object



11
12
13
# File 'app/models/effective/model_reader.rb', line 11

def read(&block)
  instance_exec(&block)
end