Skip to content

Examples #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
TheFox opened this issue Mar 26, 2017 · 4 comments
Closed

Examples #7

TheFox opened this issue Mar 26, 2017 · 4 comments

Comments

@TheFox
Copy link

TheFox commented Mar 26, 2017

Where can I find examples of a real usecase? spec/integration/adapter_spec.rb is not really an example code. It is not helpful. How can I create a new yaml file for a Relation using the YAML Gateway?

@flash-gordon
Copy link
Member

@TheFox there are two basic options you have: define all relations in a single file or create separate files per relation.
In the first case a complete example would be

data.yml

users:
  - name: 'Jane'
    email: '[email protected]'
  - name: 'John'
    email: '[email protected]'
require 'rom-yaml'

rom = ROM.container(:yaml, File.join(__dir__, 'data.yml')) do |c|
  c.relation(:users) do
    schema do
      attribute :name, ROM::Types::String
      attribute :email, ROM::Types::String
    end
  end
end

p rom.relation(:users).to_a
# [{:name=>"Jane", :email=>"[email protected]"}, {:name=>"John", :email=>"[email protected]"}]

or as always you can make a separate class

require 'rom-yaml'

class Users < ROM::Relation[:yaml]
  dataset :users

  schema do
    attribute :name, ROM::Types::String
    attribute :email, ROM::Types::String
  end
end

rom = ROM.container(:yaml, File.join(__dir__, 'data.yml')) do |c|
  c.register_relation(Users)
end

p rom.relation(:users).to_a
# [{:name=>"Jane", :email=>"[email protected]"}, {:name=>"John", :email=>"[email protected]"}]

@TheFox
Copy link
Author

TheFox commented Mar 26, 2017

@flash-gordon Thank you for your fast response. How would it look like if I doesn't already have a data.yml, when creating a new user via the Repository? Something like create_table for rom-sql but for YAML. ROM complains when I don't have a data.yml in the first place.

@flash-gordon
Copy link
Member

@TheFox at this point this is not possible because the adapter is lack of commands, I filed an issue about this #8

@TheFox
Copy link
Author

TheFox commented Mar 26, 2017

@flash-gordon I see. Thank you for your help.

@TheFox TheFox closed this as completed Mar 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants