Module: CrystalApi::Attributes::ClassMethods
- Defined in:
- lib/crystal_api/attributes.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Instance Method Summary collapse
- #array_attribute(attribute_name, type = :object) ⇒ Object
- #attribute(attribute_name) ⇒ Object
- #boolean_attribute(attribute_name) ⇒ Object
- #embedded_attribute(attribute_name, type = :object) ⇒ Object
- #from_json(json_hash) ⇒ Object
- #get_root_element ⇒ Object
- #hash_attribute(attribute_name, type) ⇒ Object
- #root_element(elem) ⇒ Object
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
10 11 12 |
# File 'lib/crystal_api/attributes.rb', line 10 def attributes @attributes end |
Instance Method Details
#array_attribute(attribute_name, type = :object) ⇒ Object
37 38 39 40 |
# File 'lib/crystal_api/attributes.rb', line 37 def array_attribute(attribute_name, type = :object) attr_reader attribute_name @attributes[attribute_name.to_s] = [:array, type] end |
#attribute(attribute_name) ⇒ Object
47 48 49 50 |
# File 'lib/crystal_api/attributes.rb', line 47 def attribute(attribute_name) attr_reader attribute_name @attributes[attribute_name.to_s] = :unknown end |
#boolean_attribute(attribute_name) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/crystal_api/attributes.rb', line 19 def boolean_attribute(attribute_name) attr_reader attribute_name @attributes[attribute_name.to_s] = :boolean if matches = attribute_name.to_s.match(/^is_(.*)$/) define_method("#{matches[1]}?") do send(attribute_name) end else alias_method "#{attribute_name}?", attribute_name end end |
#embedded_attribute(attribute_name, type = :object) ⇒ Object
32 33 34 35 |
# File 'lib/crystal_api/attributes.rb', line 32 def (attribute_name, type = :object) attr_reader attribute_name @attributes[attribute_name.to_s] = [:embedded, type] end |
#from_json(json_hash) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/crystal_api/attributes.rb', line 61 def from_json(json_hash) json_attributes = json_hash.fetch(get_root_element, json_hash) = json_attributes.delete("_embedded") || {} attrs = attributes.inject({}) do |acc, (attr, type)| if Array(type).first == :embedded val = [attr] else val = json_attributes[attr] end acc[attr] = val unless val.nil? acc end new(attrs) end |
#get_root_element ⇒ Object
56 57 58 59 |
# File 'lib/crystal_api/attributes.rb', line 56 def get_root_element klass = self @root_element || raise(NoRootElementDefined.new("No root element was defined for #{klass.name}")) end |
#hash_attribute(attribute_name, type) ⇒ Object
42 43 44 45 |
# File 'lib/crystal_api/attributes.rb', line 42 def hash_attribute(attribute_name, type) attr_reader attribute_name @attributes[attribute_name.to_s] = [:hash, type] end |
#root_element(elem) ⇒ Object
52 53 54 |
# File 'lib/crystal_api/attributes.rb', line 52 def root_element(elem) @root_element = elem.to_s end |