Module: Typhoid::Attributes::ClassMethods

Defined in:
lib/typhoid/attributes.rb

Instance Method Summary collapse

Instance Method Details

#auto_init_fieldsObject



52
53
54
# File 'lib/typhoid/attributes.rb', line 52

def auto_init_fields
  @auto_init_fields || []
end

#build(klass, response) ⇒ Object



64
65
66
# File 'lib/typhoid/attributes.rb', line 64

def build(klass, response)
  builder.call(klass, response)
end

#builderObject



56
57
58
# File 'lib/typhoid/attributes.rb', line 56

def builder
  Builder
end

#field(*field_names) ⇒ Object

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
# File 'lib/typhoid/attributes.rb', line 33

def field(*field_names)
  raise ArgumentError, "Must specify at least one field" if field_names.length == 0
  @auto_init_fields ||= []
  field_names.each do |field_name|
    define_accessor field_name
    @auto_init_fields << field_name.to_sym
  end
end

#load_values(object, response) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/typhoid/attributes.rb', line 68

def load_values(object, response)
  object.tap { |obj|
    error = nil
    begin
      obj.load_values(parser.call response.body)
    rescue ReadError => e
      error = e
    ensure
      obj.after_build response, error
    end
  }
end

#parserObject



60
61
62
# File 'lib/typhoid/attributes.rb', line 60

def parser
  Parser
end