jsonapi-rb / jsonapi-deserializable

Conveniently deserialize JSON API payloads into custom hashes.
http://jsonapi-rb.org
MIT License
26 stars 20 forks source link

New DSL. #13

Closed beauby closed 7 years ago

beauby commented 7 years ago

Got rid of the whole configuration thing. Deserialization now works by whitelisting, with a simple DSL. Example:

class DeserializableArticle < JSONAPI::Deserializable::Resource
  id # Deserialize the id, by default under the `id` key. If you omit this, the id will not be
      # part of the deserialized payload.
  type { |t| Hash[resource_type: t] } # Deserialize the type by building a custom hash
  attribute :title # Deserialize the title (by default under the `title` key).
  attribute(:date) { |date| Hash[created_at: date] } # Deserialize the date, but under a custom key.
  attributes do |val, key| # Deserialize any other attribute in a custom generic way. If no block is provided, it simply whitelists all attributes.
    Hash[key => val]
  end
  has_one :author # has_one and has_many work similarly to attribute, except the block gets yielded the following params: value, id(s), type(s), key.
  has_many :comments
end
codecov[bot] commented 7 years ago

Codecov Report

Merging #13 into master will not change coverage. The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master    #13   +/-   ##
=====================================
  Coverage     100%   100%           
=====================================
  Files           6      5    -1     
  Lines         177    174    -3     
=====================================
- Hits          177    174    -3
Impacted Files Coverage Δ
lib/jsonapi/deserializable/resource/dsl.rb 100% <100%> (ø) :arrow_up:
lib/jsonapi/deserializable/resource.rb 100% <100%> (ø) :arrow_up:
lib/jsonapi/deserializable/relationship.rb 100% <100%> (ø) :arrow_up:
lib/jsonapi/deserializable/relationship/dsl.rb 100% <100%> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 65e6a1a...117088a. Read the comment docs.